严格模式重写arguments没有报错?

严格模式重写arguments会导致语法错误,代码将不会执行。我试了一下没有报错啊

clipboard.png

clipboard.png

阅读 3k
2 个回答

http://www.ecma-international...

Strict mode 跟 arguments 相关的所有点

  • The identifier eval or arguments may not appear as the LeftHandSideExpression of an Assignment operator (12.15) or of a UpdateExpression (12.4) or as the UnaryExpression operated upon by a Prefix Increment (12.4.6) or a Prefix Decrement (12.4.7) operator.

  • Arguments objects for strict mode functions define non-configurable accessor properties named "caller" and "callee" which throw a TypeError exception on access (9.2.7).

  • Arguments objects for strict mode functions do not dynamically share their array indexed property values with the corresponding formal parameter bindings of their functions. (9.4.4).

  • For strict mode functions, if an arguments object is created the binding of the local identifier arguments to the arguments object is immutable and hence may not be the target of an assignment expression. (9.2.12).

  • It is a SyntaxError if the IdentifierName eval or the IdentifierName arguments occurs as a BindingIdentifier within strict mode code (12.1.1).

  • It is a SyntaxError if a TryStatement with a Catch occurs within strict mode code and the Identifier of the Catch production is eval or arguments (13.15.1).

  • An implementation may not extend, beyond that defined in this specification, the meanings within strict mode functions of properties named caller or arguments of function instances. ECMAScript code may not create or modify properties with these names on function objects that correspond to strict mode functions (16.2).

所以你试试 arguments = 100 就明白了。

引自MDN

第二,严格模式下,参数的值不会随 arguments 对象的值的改变而变化。在正常模式下,对于第一个参数是 arg 的函数,对 arg 赋值时会同时赋值给 arguments[0],反之亦然(除非没有参数,或者 arguments[0] 被删除)。严格模式下,函数的 arguments 对象会保存函数被调用时的原始参数。arguments[i] 的值不会随与之相应的参数的值的改变而变化,同名参数的值也不会随与之相应的 arguments[i] 的值的改变而变化。

function f(a){
  "use strict";
  a = 42;
  return [a, arguments[0]];
}
var pair = f(17);
console.assert(pair[0] === 42);
console.assert(pair[1] === 17);

也就是说严格模式下是可以给arguments赋值的,arguments用来只是纪录传入时num1,num2的值,并不会因为num1,num2在函数体里发生改变而一起改变,也不会因为你给arguments做了修改而影响num1,和num2的值。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题