非严格模式与严格模式下的arguments
arguments对象是所有(非箭头)函数中都可用的局部变量。你可以使用arguments对象在函数中引用函数的参数。
非严格模式下,对形参重新赋值会改变arguments的值
function test(a,b){
a = [].shift.call(arguments);
console.log(Array.from(arguments));
}
test(1,2);// [1];
function test(a,b){
c = [].shift.call(arguments);
console.log(Array.from(arguments));
}
test(1,2);// [2]
严格模式,函数的 arguments 对象会保存函数被调用时的原始参数。arguments[i] 的值不会随与之相应的参数的值的改变而变化,同名参数的值也不会随与之相应的 arguments[i] 的值的改变而变化。
function test(a,b){
"use strict";
a = [].shift.call(arguments);
console.log(Array.from(arguments));
}
test(1,2);//[2]
扩展:
arguments对象可以与剩余参数、默认参数和解构赋值参数结合使用。
当非严格模式中的函数没有包含剩余参数、默认参数和解构赋值,那么arguments对象中的值会跟踪参数的值(反之亦然)。当非严格模式中的函数有包含剩余参数、默认参数和解构赋值,那么arguments对象中的值不会跟踪参数的值(反之亦然);
在严格模式下,剩余参数、默认参数和解构赋值参数的存在不会改变 arguments对象的行为
参考:https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/arguments
https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Strict_mode
1条评论
非技术的路过。