最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript function declaration with same arguments - Stack Overflow

programmeradmin1浏览0评论

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 ?

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 ?

Share Improve this question edited Mar 25, 2024 at 6:32 VLAZ 29k9 gold badges62 silver badges83 bronze badges asked Mar 21, 2016 at 13:01 MarymonMarymon 2471 gold badge2 silver badges8 bronze badges 1
  • You should try using strict mode for these. JSFiddle – Rajesh Commented Mar 21, 2016 at 13:08
Add a comment  | 

4 Answers 4

Reset to default 7

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

发布评论

评论列表(0)

  1. 暂无评论