By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,696 Members | 1,422 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,696 IT Pros & Developers. It's quick & easy.

2 same function name ..

P: 3
can i have 2 function with same function name but different argument in a javascript?

my code are as followings:-

Expand|Select|Wrap|Line Numbers
  1.     function crtHidObj(name,value,idx){
  2.         crtHidObj(name,value);
  3.         createCookie("CollectionRuleCrit."+name,value,100);
  4.         }
  5.  
  6.     function crtHidObj(name,value){
  7.         f=document.frmC5W002;
  8.         var hidObj = document.createElement("INPUT");
  9.         hidObj.setAttribute("type","hidden");
  10.         hidObj.setAttribute("name",name);
  11.         hidObj.setAttribute("value",value);
  12.         f.appendChild(hidObj);
  13.     }

Anyone know? pls reply ... many thanks !!~
Jan 28 '08 #1
Share this Question
Share on Google+
3 Replies


gits
Expert Mod 5K+
P: 5,327
nope ... that will not work (the second one overwrites the first one) ... why should the functions have the same name?

kind regards
Jan 28 '08 #2

P: 1
IE will default take the second one. just using different name will do
Jan 15 '17 #3

gits
Expert Mod 5K+
P: 5,327
well - it doesn't have anything to do with a browser or its version. declaring a variable or a function with the same name overwrites the previous one. to give a clear answer i assume that the purpose of the original question was to define a function that can have optional arguments which could be solved for example like this:

Expand|Select|Wrap|Line Numbers
  1. function foo(arg1, arg2, arg3) {
  2.     if (typeof arg3 == 'undefined') {
  3.         // code that only requires arg1 and arg2
  4.     } else {
  5.         // code that requires arg3 
  6.     }
  7. }
  8.  
  9. // now method foo may be called as:
  10.  
  11. foo(1, 2);
  12.  
  13. // or:
  14.  
  15. foo(1, 2, 3);
Mar 15 '17 #4

Post your reply

Sign in to post your reply or Sign up for a free account.