浅析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 5use 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对象被禁止为函数参数创建gettersetter方法。

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`的内容~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值