浅析js中的arguments
文章目录
arguments就是传递进函数的参数列表,它是一个类数组对象-Array-Like Object.
类数组对象,简单来说就是拥有length属性,如我们常用的NodeList,arguments,但却不能使用数组方法,如forEach,map等
1. length
var foo = function () {
console.log(arguments.length)
}
foo() // 0
foo(1, 2) // 2
2. Function.arguments永远不要使用,已被废除,直接使用arguemnts对象
3. arguments对象会被参数或变量arguments覆盖
var foo = function () {
console.log(arguments) // [10]
}
foo(10)
var bar = function (arguments) {
console.log(arguments) // 10
}
bar(10)
var foo = function () {
console.log(arguments) // [10]
}
foo(10)
var bar = function (arguments) {
arguments = 20
console.log(arguments) // 20
}
bar(20)
4.将arguments对象转化为数组对象
arguments = [].slice.call(arguments)arguments = Array.from(arguments)待补充(暂时没想到其余简洁的写法.)
5. arguments.callee指向当前正在被执行的函数 -- 应当停止使用
最常见的一个例子
阶乘
var foo = function (x) {
return x >= 1 ? x * arguments.callee(x-1) : 1
}
console.log(foo(3)) // 6
关于它更详尽的解释,我更愿意给你推荐下面这个链接。
link-可能需要自备梯子
值得注意的一点是,在下面我们提到的ES5 use strict模式下,使用arguments.callee会报错.
5. arguments数量 >= parameters数量 -- 双向绑定
var foo = function (x, y) {
arguments[0] = 10
console.log(arguments[0], x) //10, 10
x = 20
console.log(arguments[0], x) //20, 20
arguments[1] = 30
console.log(arguments[1], y) //30, 30
y = 40
console.log(arguments[1], y) //40, 40
}
foo(1, 2)
6. arguments数量 < parameters数量 -- 缺省参数未双向绑定
var foo = function (x, y) {
arguments[0] = 10
console.log(arguments[0], x) //10, 10
x = 20
console.log(arguments[0], x) //20, 20
arguments[1] = 30
console.log(arguments[1], y) //30, undefined
y = 40
console.log(arguments[1], y) //40, 40
}
foo(1)
7. use strict
以上2种情况,会给我们在使用时稍不注意,不去区分,就会犯错。
不过还好,我们可以通过ES 5的use strict特性进行统一.
//对应上面第一种情况
var foo = function (x, y) {
'use strict'
arguments[0] = 10
console.log(arguments[0], x) //10, 1
x = 20
console.log(arguments[0], x) //10, 20
arguments[1] = 30
console.log(arguments[1], y) //30, 2
y = 40
console.log(arguments[1], y) //30, 40
}
foo(1, 2)
//对应上面第二种情况
var foo = function (x, y) {
'use strict'
arguments[0] = 10
console.log(arguments[0], x) //10, 1
x = 20
console.log(arguments[0], x) //10, 20
arguments[1] = 30
console.log(arguments[1], y) //30, undefined
y = 40
console.log(arguments[1], y) //30, 40
}
foo(1)
因为在严格模式下,arguments对象被禁止为函数参数创建getter和setter方法。
8. 使用...Rest代替
...Rest只能被定义在函数参数的最后一个位置,传进函数的多余参数都将传进这个数组
var foo = function (x, y, ...z) {
console.log(z) // [3, 4, 5]
console.log(Array.isArray(z)) // true
}
foo(1, 2, 3, 4, 5)
结束语
欢迎指正错误和提出更多的关于`arguments`的内容~

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论