1.定义

拥有一个length属性和若干索引属性的对象,例如函数内部的arguments,一些DOM获取节点的方法,比如:ducoment.getElementsByTagName()的返回值都是类数组对象.

2.调用原生数组方法

通过call或apply的方式间接调用数组方法

  1. function T() {
  2. let arg = arguments
  3. let argArray = Array.prototype.slice.call(arg,0)
  4. let argArray2 = [].indexOf.call(arg,'a')
  5. }

3.类数组转换为数组

  1. //1
  2. [].slice.call(arraylike,0)
  3. //2
  4. [].splice.call(arraylike,0)
  5. //3
  6. Array.from(arraylike)
  7. //4
  8. [...arraylike]
  9. //5
  10. [].concat.apply([],arraylike)

4.arguments

4.1 length

Arguments对象的length属性,表示实参的长度,举个例子:

  1. function foo(a,b,c) {
  2. console.log(foo.length) //形参长度
  3. console.log(arguments.length) //实参长度
  4. }

4.2 callee

通过arguments.callee属性可以调用arguments所属的函数.

4.3 arguments和对应参数的绑定

传入的参数,实参和 arguments 的值会共享,当没有传入时,实参与 arguments 值不会共享,除此之外,以上是在非严格模式下,如果是在严格模式下,实参和 arguments 是不会共享的。

  1. function foo(name, age, sex, hobbit) {
  2. console.log(name, arguments[0]); // name name
  3. // 改变形参
  4. name = 'new name';
  5. console.log(name, arguments[0]); // new name new name
  6. // 改变arguments
  7. arguments[1] = 'new age';
  8. console.log(age, arguments[1]); // new age new age
  9. // 测试未传入的是否会绑定
  10. console.log(sex); // undefined
  11. sex = 'new sex';
  12. console.log(sex, arguments[2]); // new sex undefined
  13. arguments[3] = 'new hobbit';
  14. console.log(hobbit, arguments[3]); // undefined new hobbit
  15. }
  16. foo('name', 'age')