Skip to content

Instantly share code, notes, and snippets.

@kerryChen95
Last active December 15, 2015 03:09
Show Gist options
  • Save kerryChen95/5192785 to your computer and use it in GitHub Desktop.
Save kerryChen95/5192785 to your computer and use it in GitHub Desktop.
Vaules of indexes on `arguments` object are shared with values of formal parameters
var objInGlobal = {
  a: 'a',
  b: 'b'
};

(function (baseTypeVar, referTypeVar, notPassedArg) {
  baseTypeVar = 2;
  console.log(baseTypeVar === arguments[0]); // true
  
  console.log(referTypeVar === objInGlobal); // true
  referTypeVar = {
    c: 'c',
    d: 'd'
  };
  console.log(referTypeVar === arguments[1]); // true
  console.log(referTypeVar === objInGlobal); //false
  arguments[1] = {
    e: 'e',
    f: 'f'
  };
  console.log(referTypeVar === arguments[1]); // true
  
  console.log(2 in arguments); // false
  notPassedArg = 1;
  console.log(notPassedArg === arguments[2]); // false
  arguments[2] = 2;
  console.log(notPassedArg === arguments[2]); // false
})(1, objInGlobal);

Vaules of indexes on arguments object are shared with values of formal parameters, which means they are always equal even when you override values of indexes on arguments or ones of formal parameters.

However, for those parameters that no value passed to, values of related indexes on arguments object are not shared, since these indexes are not defined on arguments when entering function

From ECMA-262-3, but not JavaScript interpreter implementation.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment