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