Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Javascript choose nearest method physically when it comes to overloading? [duplicate]

Tags:

javascript

While I was just playing around with JS, I wrote following snippet:

function checkArgs(abc,nbn,jqrs){
    console.log("overloaded " +arguments.length);
}

function checkArgs(abc){
    console.log(arguments.length);
}

checkArgs("aa","lll","pp");

I see output as " 3 " , however I was expecting out put as "overloaded 3". But I does not happen , however if I just swap the postions of those method, it does happen.

 function checkArgs(abc){
        console.log(arguments.length);
    }


  function checkArgs(abc,nbn,jqrs){
        console.log("overloaded " +arguments.length);
    }    

    checkArgs("aa","lll","pp");

Whats the rationale behind it?

like image 728
Orion Avatar asked Jul 24 '16 20:07

Orion


1 Answers

There is no function overloading in javascript.

The latest method declaration always overwrites the previous one with the same name. No error thrown.

Also functions in javascript are all variadic. Any number of arguments can be passed regardless of the function signature.

like image 102
eltonkamami Avatar answered Nov 14 '22 23:11

eltonkamami