Javascript function declaration with same arguments
Asked Answered
H

4

13

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 ?

Hyla answered 21/3, 2016 at 13:1 Comment(1)
You should try using strict mode for these. JSFiddlePolloch
S
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.

Straightway answered 21/3, 2016 at 13:5 Comment(3)
But into strict mode I can declare same variable twice (I just tested). But its not allowing same for function arguments.Hyla
@Hyla yes, that's correct. Redundant 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.Straightway
It's essentially undefined variable being assigned to other undefined variable issue internally.Deledda
T
3

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.

Triage answered 21/3, 2016 at 13:29 Comment(0)
D
1

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

Deledda answered 21/3, 2016 at 13:5 Comment(0)
H
0

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

Harkness answered 21/3, 2016 at 13:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.