TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed
JavaScript 嚴格模式下才會出現的異常:“在嚴格模式函式或對其的呼叫引數物件上,無法訪問'caller'、'callee'和'arguments'屬性”,當使用已棄用的 arguments.callee、Function.prototype.caller 或 Function.prototype.arguments 屬性時會發生此異常。
訊息
TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them (V8-based & Firefox) TypeError: 'arguments', 'callee', and 'caller' cannot be accessed in this context. (Safari)
錯誤型別
哪裡出錯了?
在嚴格模式下,arguments.callee、Function.prototype.caller 或 Function.prototype.arguments 屬性被使用,但不應被使用。它們已被棄用,因為它們會洩露函式呼叫者,是非標準的,難以最佳化,並且可能是一個對效能有害的特性。
示例
已棄用的 function.caller 或 arguments.callee
Function.prototype.caller 和 arguments.callee 已被棄用(更多資訊請參閱參考文章)。
js
"use strict";
function myFunc() {
if (myFunc.caller === null) {
return "The function was called from the top!";
}
return `This function's caller was ${myFunc.caller}`;
}
myFunc();
// TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them
Function.prototype.arguments
Function.prototype.arguments 已被棄用(更多資訊請參閱參考文章)。
js
"use strict";
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}`);
// TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them