Author Topic: What happens when multiple js functions with same name exist on a Html Page?  (Read 102 times)

0 Members and 1 Guest are viewing this topic.

Offline h2obubbler

  • Full Member
  • *
  • Posts: 228
  • Karma: +0/-0
  • Life is like a box of chocolate.
    • View Profile
Am just wondering, What happens when multiple js functions with same name exist on a Html Page? Which of the functions are used or being called ? ANy idea?
« Last Edit: August 20, 2014, 08:31:04 AM by h2obubbler »

Techronnati | where technology never sleeps


Mountain View

Offline CyborgRepublic

  • Full Member
  • *
  • Posts: 203
  • Karma: +0/-0
    • View Profile
based from experience, kung sino yung last na nadefine or the latest one, it's the one that is executed

For Example may ganito kang code:

Code: [Select]
<script type="text/javascript" src="../scripts/Admin.js"></script>
<script type="text/javascript" src="../scripts/TEST.js"></script>

... tapos both have validateuser() function. Kung alin yung nadefine sa TEST.js sya yung tatawagin.

Its not best practice to use same function names across different js Files. Better to consolidate everthing para mas maayos ang code mo

Offline Corps

  • Sr. Member
  • *
  • Posts: 276
  • Karma: +0/-0
    • View Profile
tama si CyborgRepublic, in JavaScript the last one defined will always be the one run regardless of the number of parameters.

Offline Berto

  • Full Member
  • *
  • Posts: 138
  • Karma: +0/-0
    • View Profile
thanks for sharing....

I thought pag ganito, mag eerror yung code, hindi pala.

Offline MrSpecialist

  • Sr. Member
  • *
  • Posts: 345
  • Karma: +1/-0
  • I'm an expert!
    • View Profile
bakit kaya yung latest ang kinukuha ni javascript sa loading? inoverride nya yung luma pagkaload?

Does this work to all browsers?

Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 359
  • Karma: +1/-0
    • View Profile
Is there a way like for instance gagamit ako ng same function pero iba yung parameters nya inside?

Is there such as thing as polymorphism sa javascript?


Offline MrSpecialist

  • Sr. Member
  • *
  • Posts: 345
  • Karma: +1/-0
  • I'm an expert!
    • View Profile
Is there a way like for instance gagamit ako ng same function pero iba yung parameters nya inside?

Is there such as thing as polymorphism sa javascript?



To answer your question, wala pong polymorphism sa javascript. yun ang draw back nya. not unless there's a mechanism for new browsers to allow this functionality to exist. or a patch maybe?


Offline JusticeLeague

  • Sr. Member
  • *
  • Posts: 359
  • Karma: +1/-0
    • View Profile
Is there a way like for instance gagamit ako ng same function pero iba yung parameters nya inside?

Is there such as thing as polymorphism sa javascript?



To answer your question, wala pong polymorphism sa javascript. yun ang draw back nya. not unless there's a mechanism for new browsers to allow this functionality to exist. or a patch maybe?



If this is the case, so hindi pala object oriented ang Javascript? i thought para syang java na pwede mag polymorphism.

Anyway so what kind of approach ang pwede?

Offline MrSpecialist

  • Sr. Member
  • *
  • Posts: 345
  • Karma: +1/-0
  • I'm an expert!
    • View Profile
Is there a way like for instance gagamit ako ng same function pero iba yung parameters nya inside?

Is there such as thing as polymorphism sa javascript?



To answer your question, wala pong polymorphism sa javascript. yun ang draw back nya. not unless there's a mechanism for new browsers to allow this functionality to exist. or a patch maybe?



If this is the case, so hindi pala object oriented ang Javascript? i thought para syang java na pwede mag polymorphism.

Anyway so what kind of approach ang pwede?


How about gagamit ka ng concept ng namespacing like sample:


//Count.js:

Quote
var Count = {
  add: function add() {
  },
  [additional methods in the Count object]
};


// PriceImpl.js

Code: [Select]
var Price = {
  add: function add () {
  },
  [additional methods for the Price implementation]
};

Then call methods like using syntax - Namespace.method , so in this case Price.add() etc.


 

Related Topics

  Subject / Started by Replies Last post
0 Replies
157 Views
Last post July 28, 2008, 09:14:01 AM
by Corps
0 Replies
182 Views
Last post August 31, 2008, 11:08:12 PM
by JusticeLeague
1 Replies
72 Views
Last post April 06, 2010, 10:58:19 PM
by TDelight
0 Replies
53 Views
Last post September 16, 2014, 09:20:33 AM
by MrSpecialist
1 Replies
209 Views
Last post September 09, 2016, 08:33:28 AM
by nomadic

Posting Disclaimer: Any individual may post a message in this forum and may do so anonymously. Therefore, the sole author is exclusively and entirely responsible for all opinions in that message. They do not represent the official opinions of Techronnati, its administrators or moderators or the Techronnati Management. Techronnati is merely acting as an impartial conduit for constitutionally protected free speech and is not responsible and will not be held liable for the content of such messages. All images and service logos are trademarks of their respective owners.