2 same function name ..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jacelyn2211
    New Member
    • Jan 2008
    • 3

    2 same function name ..

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

    my code are as followings:-

    Code:
        function crtHidObj(name,value,idx){
            crtHidObj(name,value);
            createCookie("CollectionRuleCrit."+name,value,100);
            }
        
        function crtHidObj(name,value){
            f=document.frmC5W002;
            var hidObj = document.createElement("INPUT");
            hidObj.setAttribute("type","hidden");
            hidObj.setAttribute("name",name);
            hidObj.setAttribute("value",value);
            f.appendChild(hidObj);
        }

    Anyone know? pls reply ... many thanks !!~
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5390

    #2
    nope ... that will not work (the second one overwrites the first one) ... why should the functions have the same name?

    kind regards

    Comment

    • Winson Tan
      New Member
      • Jan 2017
      • 1

      #3
      IE will default take the second one. just using different name will do

      Comment

      • gits
        Recognized Expert Moderator Expert
        • May 2007
        • 5390

        #4
        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:

        Code:
        function foo(arg1, arg2, arg3) {
            if (typeof arg3 == 'undefined') {
                // code that only requires arg1 and arg2
            } else {
                // code that requires arg3 
            }
        }
        
        // now method foo may be called as:
        
        foo(1, 2);
        
        // or:
        
        foo(1, 2, 3);

        Comment

        Working...