このページはコミュニティーの尽力で英語から翻訳されました。MDN Web Docs コミュニティーについてもっと知り、仲間になるにはこちらから。

View in English Always switch to English

TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed

JavaScript の 厳格モードでのみ発生する例外 "'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them" は、非推奨の 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)

エラー型

TypeError です。厳格モードでのみ発生します。

エラーの原因

厳格モードで、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

関連情報