arguments 对象
Baseline 广泛可用 *
试一试
function func1(a, b, c) {
console.log(arguments[0]);
// Expected output: 1
console.log(arguments[1]);
// Expected output: 2
console.log(arguments[2]);
// Expected output: 3
}
func1(1, 2, 3);
描述
注意: 在现代代码中,应该优先使用剩余参数。
arguments 对象是一个局部变量,在所有非箭头函数中都可用。你可以使用函数的 arguments 对象在函数内部引用该函数的参数。它包含函数调用时传入的每个参数的条目,第一个条目的索引为 0。
例如,如果一个函数传入 3 个参数,你可以这样访问它们
arguments[0]; // first argument
arguments[1]; // second argument
arguments[2]; // third argument
arguments 对象对于以比正式声明接受的参数更多的方式调用的函数(称为可变参数函数,例如 Math.min())很有用。此示例函数接受任意数量的字符串参数并返回最长的一个
function longestString() {
let longest = "";
if (arguments.length === 0) {
throw new TypeError("At least one string is required");
}
for (const arg of arguments) {
if (arg.length > longest.length) {
longest = arg;
}
}
return longest;
}
你可以使用arguments.length 来计算函数被调用时传入了多少个参数。如果你想计算函数声明接受多少个参数,请检查该函数的 length 属性。
赋值给索引
每个参数索引也可以设置或重新分配
arguments[1] = "new value";
只有简单参数(即没有剩余参数、默认参数或解构参数)的非严格模式函数将参数的新值与 arguments 对象同步,反之亦然
function func(a) {
arguments[0] = 99; // updating arguments[0] also updates a
console.log(a);
}
func(10); // 99
function func2(a) {
a = 99; // updating a also updates arguments[0]
console.log(arguments[0]);
}
func2(10); // 99
如果非严格模式函数传入了剩余参数、默认参数或解构参数,则在函数体中分配给参数的新值将不会与 arguments 对象同步。相反,具有复杂参数的非严格模式函数中的 arguments 对象将始终反映函数调用时传递给函数的值。
function funcWithDefault(a = 55) {
arguments[0] = 99; // updating arguments[0] does not also update a
console.log(a);
}
funcWithDefault(10); // 10
function funcWithDefault2(a = 55) {
a = 99; // updating a does not also update arguments[0]
console.log(arguments[0]);
}
funcWithDefault2(10); // 10
// An untracked default parameter
function funcWithDefault3(a = 55) {
console.log(arguments[0]);
console.log(arguments.length);
}
funcWithDefault3(); // undefined; 0
这与所有严格模式函数表现出的行为相同,无论它们传入的参数类型如何。也就是说,在函数体中为参数分配新值永远不会影响 arguments 对象,为 arguments 索引分配新值也不会影响参数的值,即使函数只有简单参数。
注意: 你不能在接受剩余参数、默认参数或解构参数的函数定义体内编写 "use strict"; 指令。这样做会抛出语法错误。
arguments 是一个类数组对象
arguments 是一个类数组对象,这意味着 arguments 具有 length 属性和从零开始索引的属性,但它不具有 Array 的内置方法,例如 forEach() 或 map()。但是,它可以使用 slice()、Array.from() 或扩展语法转换为真正的 Array。
const args = Array.prototype.slice.call(arguments);
// or
const args = Array.from(arguments);
// or
const args = [...arguments];
对于常见的用例,将其用作类数组对象就足够了,因为它既可迭代又具有 length 和数字索引。例如,Function.prototype.apply() 接受类数组对象。
function midpoint() {
return (
(Math.min.apply(null, arguments) + Math.max.apply(null, arguments)) / 2
);
}
console.log(midpoint(3, 1, 4, 1, 5)); // 3
属性
arguments.callee已废弃-
引用 arguments 所属的当前正在执行的函数。在严格模式下禁用。
arguments.length-
传递给函数的参数数量。
arguments[Symbol.iterator]()-
返回一个新的数组迭代器对象,其中包含
arguments中每个索引的值。
示例
定义一个连接多个字符串的函数
此示例定义了一个连接多个字符串的函数。该函数的唯一形式参数是包含分隔要连接项的字符的字符串。
function myConcat(separator) {
const args = Array.prototype.slice.call(arguments, 1);
return args.join(separator);
}
你可以向此函数传递任意数量的参数。它使用列表中的每个参数返回一个字符串列表
myConcat(", ", "red", "orange", "blue");
// "red, orange, blue"
myConcat("; ", "elephant", "giraffe", "lion", "cheetah");
// "elephant; giraffe; lion; cheetah"
myConcat(". ", "sage", "basil", "oregano", "pepper", "parsley");
// "sage. basil. oregano. pepper. parsley"
定义一个创建 HTML 列表的函数
此示例定义了一个创建包含 HTML 列表的字符串的函数。该函数的唯一形式参数是一个字符串,如果列表是无序(带项目符号),则为 "u",如果列表是有序(带编号),则为 "o"。该函数定义如下
function list(type) {
let html = `<${type}l><li>`;
const args = Array.prototype.slice.call(arguments, 1);
html += args.join("</li><li>");
html += `</li></${type}l>`; // end list
return html;
}
你可以向此函数传递任意数量的参数,它会将每个参数作为列表项添加到指定类型的列表中。例如
list("u", "One", "Two", "Three");
// "<ul><li>One</li><li>Two</li><li>Three</li></ul>"
将 typeof 与 arguments 一起使用
当与 arguments 一起使用时,typeof 运算符返回 'object'
console.log(typeof arguments); // 'object'
可以通过索引 arguments 来确定单个参数的类型
console.log(typeof arguments[0]); // returns the type of the first argument
规范
| 规范 |
|---|
| ECMAScript® 2026 语言规范 # sec-arguments-exotic-objects |
浏览器兼容性
加载中…