Skip to content

Instantly share code, notes, and snippets.

@dailc
Created May 26, 2017 01:40
Show Gist options
  • Save dailc/3e32ce99ce6369dbce0ba159dc14120c to your computer and use it in GitHub Desktop.
Save dailc/3e32ce99ce6369dbce0ba159dc14120c to your computer and use it in GitHub Desktop.
[JS的arguments详解] JS中的arguments操作注意项 tags:arguments
## 原则
* 永远不要修改arguments对象
* 使用[].slice.call(arguments)得到一个arguments对象的拷贝,然后对拷贝进行修改等操作
### 一些严格模式下arguments常见错误
```
function strict(x) {
"use strict";
arguments[0] = "modified";
return x === arguments[0];
}
function nonStrict(x) {
arguments[0] = "modified";
return x === arguments[0];
}
strict("unmodified"); // false
nonStrict("unmodified"); // true
```
严格模式下,修改arguments对象,不会同时修改括号中的形参
但是非严格模式下,括号中的形参也会被对应修改,所以这样经常会造成误解
为了统一,还是尽量避免直接操作arguments,要操作请单独进行一份拷贝
### 参考
* http://blog.csdn.net/dm_vincent/article/details/39393919
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment