Function.prototype.arguments
Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.
Non-standard: This feature is not standardized. We do not recommend using non-standard features in production, as they have limited browser support, and may change or be removed. However, they can be a suitable alternative in specific cases where no standard option exists.
Note:
Thuộc tính arguments của các đối tượng Function đã bị deprecated. Cách được khuyến nghị để truy cập đối tượng arguments là tham chiếu đến biến arguments có sẵn bên trong các hàm.
Accessor property arguments của các instance Function trả về các đối số được truyền vào hàm này. Đối với các hàm strict, arrow, async, và generator, việc truy cập thuộc tính arguments sẽ ném ra TypeError.
Mô tả
Giá trị của arguments là một đối tượng dạng mảng tương ứng với các đối số được truyền vào hàm.
Trong trường hợp đệ quy, tức là nếu hàm f xuất hiện nhiều lần trên call stack, giá trị của f.arguments đại diện cho các đối số tương ứng với lần gọi gần nhất của hàm.
Giá trị của thuộc tính arguments thường là null nếu không có lần gọi nào đang thực thi của hàm (tức là hàm đã được gọi nhưng chưa trả về).
Lưu ý rằng hành vi duy nhất được đặc tả bởi ECMAScript specification là Function.prototype có một accessor arguments ban đầu luôn ném ra TypeError cho bất kỳ yêu cầu get hay set nào (được gọi là "poison pill accessor"), và các implementation không được phép thay đổi ngữ nghĩa này cho bất kỳ hàm nào ngoài các hàm plain function không phải strict mode. Hành vi thực tế của thuộc tính arguments, nếu nó khác với việc ném lỗi, được định nghĩa bởi từng implementation. Ví dụ, Chrome định nghĩa nó là own data property, trong khi Firefox và Safari mở rộng accessor poison-pill Function.prototype.arguments ban đầu để xử lý đặc biệt các giá trị this là hàm không phải strict mode.
(function f() {
if (Object.hasOwn(f, "arguments")) {
console.log(
"arguments is an own property with descriptor",
Object.getOwnPropertyDescriptor(f, "arguments"),
);
} else {
console.log(
"f doesn't have an own property named arguments. Trying to get f.[[Prototype]].arguments",
);
console.log(
Object.getOwnPropertyDescriptor(
Object.getPrototypeOf(f),
"arguments",
).get.call(f),
);
}
})();
// In Chrome:
// arguments is an own property with descriptor {value: Arguments(0), writable: false, enumerable: false, configurable: false}
// In Firefox:
// f doesn't have an own property named arguments. Trying to get f.[[Prototype]].arguments
// Arguments { … }
Ví dụ
>Sử dụng thuộc tính arguments
function f(n) {
g(n - 1);
}
function g(n) {
console.log(`before: ${g.arguments[0]}`);
if (n > 0) {
f(n);
}
console.log(`after: ${g.arguments[0]}`);
}
f(2);
console.log(`returned: ${g.arguments}`);
// Logs:
// before: 1
// before: 0
// after: 0
// after: 1
// returned: null
Đặc tả kỹ thuật
Không thuộc bất kỳ tiêu chuẩn nào.