arguments 详解

arguments

arguments

类数组

有length 从0开始的数组下标 没有数组的内置方法
是非箭头函数的其他函数的内置的局部变量

callee 宿主函数

arguments.callee() 返回函数本身

Symbol.iterator 可迭代对象标志

function test(...args) {
  console.log(Array.isArray(arguments)); //false
  console.log(Array.isArray(args)); //true
}
test(1, 2, 3);

args就是数组所以也是可迭代的

arguments转换为数组

slice用在arguments身上会阻止js引擎做一些特定优化
方法一

function test() {
  var argArr = [].slice.call(arguments);
  console.log(argArr);
}
test(1, 2, 3);

在这里插入图片描述

方法二

function test() {
  var argArr = arguments.length === 1 ? [arguments[0]] : Array.apply(null, arguments);
  console.log(argArr);
}
test(1, 2, 3);

形参实参的对应关系 共享关系

function test(a) {
  arguments[0] = 10;
  console.log(a, arguments[0]); //10,10
}
test(1);
function test(a) {
  a = 100;
  console.log(a, arguments[0]); //100,100
}
test(1);
形参中但凡有一个参数有默认值 arguments都不会对应跟踪参数最终的值
function test(a = 100) {
  arguments[0] = 10;
  console.log(a, arguments[0]);//1,10
}
test(1);
function test(a, b, c = 10) {
  arguments[0] = 100;
  arguments[1] = 200;
  arguments[2] = 300;
  console.log(a, arguments[0]);//1 100
  console.log(b, arguments[1]);//2 200
  console.log(c, arguments[2]);//3 300
}
test(1, 2, 3);
当剩余参数的时候也不跟踪
function test(...args) {
  arguments[0] = 100;
  arguments[1] = 200;
  arguments[2] = 300;
  console.log(args[0], arguments[0]); //1 100
  console.log(args[1], arguments[1]); //2 200
  console.log(args[2], arguments[2]); //3 300
}
test(1, 2, 3);
参数解构的时候也不跟踪
function test({ a, b, c }) {
  arguments[0] = 100;
  arguments[1] = 200;
  arguments[2] = 300;
  console.log(a, arguments[0]); //1 100
  console.log(b, arguments[1]); //2 200
  console.log(c, arguments[2]); //3 300
}
test({ a: 1, b: 2, c: 3 });
严格模式下也不对应 不共享
function test(a, b, c) {
  'use strict';
  a = 10;
  b = 20;
  c = 30;
  console.log(a, b, c);
  console.log(arguments);
}
test(1, 2, 3);

在这里插入图片描述

function test(a, b, c) {
  'use strict';
  arguments[0] = 10;
  arguments[1] = 20;
  arguments[2] = 30;
  console.log(a, b, c);
  console.log(arguments);
}
test(1, 2, 3);

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_聪明勇敢有力气

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值