I am learning javascript myself. I found if I declare a function with same arguments it just working fine:
function func(a, b, a){
return b;
}
alert(func(1,2,3));
But if I do this :
function func(a, b, a = 5){
return b;
}
alert(func(1,2,3));
//Firebug error - SyntaxError: duplicate argument names not allowed in this context
Then its not working anymore. What is the logic behind that it was working for first equation but not for second one ?
-
You should try using strict mode for these. JSFiddleRajesh Dixit– Rajesh Dixit2016年03月21日 13:08:18 +00:00Commented Mar 21, 2016 at 13:08
4 Answers 4
ES2015 (the newest stable spec for the language) allows parameters to be declared with default values. When you do that, the language won't allow you to re-use a parameter name.
When you're not doing any parameter defaults, the language allows the old "sloppy" re-use of parameter names. If you enable "strict" mode interpretation, you'll get an error for your first example too.
3 Comments
var declarations are allowed, I suspect because too much old code would break, and because (thanks to the semantics of var) they don't really hurt anything. JavaScript has a long, strange history, and there are many things about it that may seem inconsistent.undefined variable being assigned to other undefined variable issue internally.As per the spec
- If parameterNames has any duplicate entries, let hasDuplicates be true. Otherwise, let hasDuplicates be false.
21.b
NOTE Early errors ensure that duplicate parameter names can only occur in non-strict functions that do not have parameter default values or rest parameters.
So, your JS engine ensures that if one of the parameter has default values and hasDuplicates is true then it throws an error.
Comments
According to MDN, this kind of check is done by JS internally in case of defaults
function go() {
return ":P"
}
function withDefaults(a, b = 5, c = b, d = go(), e = this,
f = arguments, g = this.value) {
return [a,b,c,d,e,f,g];
}
function withoutDefaults(a, b, c, d, e, f, g){
switch(arguments.length){
case 0:
a
case 1:
b = 5
case 2:
c = b
case 3:
d = go();
case 4:
e = this
case 5:
f = arguments
case 6:
g = this.value;
default:
}
return [a,b,c,d,e,f,g];
}
withDefaults.call({value:"=^_^="});
// [undefined, 5, 5, ":P", {value:"=^_^="}, arguments, "=^_^="]
withoutDefaults.call({value:"=^_^="});
// [undefined, 5, 5, ":P", {value:"=^_^="}, arguments, "=^_^="]
Now in your case, this is something like this -
case 0:
a
case 1:
b
case 2:
a = a
But when executing case 2, a is still not defined, and hence it through in error scenario.
See details here https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/default_parameters
Comments
Argument name must be unique; if you use same name for two arguments and then interpreter get confuses which one you want to access;
Same you added in code as comment
//Firebug error - SyntaxError: duplicate argument names not allowed in this context
"Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed." Default parameter in ES2015
Comments
Explore related questions
See similar questions with these tags.