Override Javascript Function Based on Passed Parameters

Posted by George on Stack Overflow See other posts from Stack Overflow or by George
Published on 2010-04-29T15:16:58Z Indexed on 2010/04/29 15:27 UTC
Read the original article Hit count: 630

Is it possible to override a function based on the number of parameters that you are passing into it? For instance:

function abc(name) {
    document.write ('My name is' + name);
}

function abc(name,friend) {
    document.write ('My name is' + name + 'and my best friend\'s name is' + friend);
}

So in the HTML if I just called abc(george) it would use the first version of the function, but if I called abc(george,john) it would use the second version.

There may be other ways to accomplish the example I used, but I'm just wondering if the concept is sound in javascript.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about parameters