Friday 16 June 2017

javascript - In JS, is it allowed to declared the same name of functions with different number of parameters like Java?

Before I start writing this post, I already tried the codes below;



function foo(a){
alert(a);}

function foo(a, b){
alert(a + ' ' + b);}



And in a function which called when the page is loaded, I called the two functions as this: foo(1); foo(1, 2); Then I firstly got "1 undefined" and then "1 2".



What I expected first is that I might be able to declare the same name of functions with different parameters like Java; but I'm not sure I can. At least I'm sure that declaring function foo(a, b){...} and function foo(b, c){...} is prohibited.

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...