Not signed in (Sign In)

Vanilla 1.1.4 is a product of Lussumo. More Information: Documentation, Community Support.

    • CommentAuthoradmin
    • CommentTimeMay 1st 2008
     

    Does JavaScript support Method Overloading? Please provide an explanation with your answer. This question is open for the next 2 weeks. I will post the correct answer on May 15, 2008.

    Let the games begin! :)

    • CommentAuthorskunk
    • CommentTimeMay 8th 2008
     

    I'm not that good in Javascript. I am just sinking my teeth into it.

    But I know that it does not support method overloading. You have to work around that using some lousy technique. :)

    • CommentAuthoradmin
    • CommentTimeJun 2nd 2008
     

    The official answer is NO. Technically however, it is possible. Allow me to explain if I may:

    Copy this code snippet, paste and save it, in your favorite text editor and run it. Of course, ignore any wrong/missing tags, etc:

    ================

    Untitled

    function overload() {
    var howMany = arguments.length;
    var sum;

    if(howMany == 1) {
    sum = arguments[0] + 5;
    } else {
    sum = arguments[0] + arguments[1];
    }

    alert(sum);
    }

    1 argument - overload(10)

    2 arguments - overload(10, 10)

    ================

    The trick is that every function has an "arguments" property. By accessing the number of arguments you can technically perform method overloading and get away with it ;)