简体   繁体   中英

Javascript function declaration with same arguments

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 ?

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.

As per the spec

  1. 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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM