--- title: arguments.callee slug: Web/JavaScript/Reference/Functions/arguments/callee tags: - Deprecated - JavaScript - Reference - arguments - プロパティ - 関数 translation_of: Web/JavaScript/Reference/Functions/arguments/callee ---
{{jsSidebar("Functions")}}

arguments.callee プロパティは現在実行中の関数を示します。

説明

calleearguments オブジェクトのプロパティです。関数の本体の内部で現在実行中の関数を参照するために使用します。これは関数名が不明であるとき、たとえば名前のない関数式(「無名関数」)の内部などで便利です。

警告: ECMAScript 第5版では、 strict モードにおける arguments.callee() の使用を禁止しています。関数式に名前を付けるか、関数が自身を呼び出す必要がある場合に関数宣言を使用するかして arguments.callee() の使用を避けてください。

なぜ arguments.callee は ES5 strict mode で削除されたのか

olliej による Stack Overflow の回答によれば)

古いバージョンの JavaScript では名前付きの関数式が利用できず、このため再帰の関数式を作成することができませんでした。

例えば、以下の構文は動作しました。

function factorial (n) {
    return !(n > 1) ? 1 : factorial(n - 1) * n;
}

[1, 2, 3, 4, 5].map(factorial);

しかし、

[1, 2, 3, 4, 5].map(function(n) {
    return !(n > 1) ? 1 : /*ここでどうする?*/ (n - 1) * n;
});

は動作しませんでした。これをうまくやるために arguments.callee が追加され、あなたは以下のようなことができます。

[1, 2, 3, 4, 5].map(function(n) {
    return !(n > 1) ? 1 : arguments.callee(n - 1) * n;
});

しかし、これは実際には本当にダメな解決法でした。これは(他の arguments や calleecaller の問題と組み合わさって)、一般的には、再帰のインライン化と末尾再帰が不可能になるのです(あなたはこれをトレースなどを通じて選択した場合では達成できます。しかし最良のコードであっても、逆に不要な検査においては最適未満です)。他の大きな問題としては、再帰呼び出しが別の this の値になることです。例えば、

var global = this;

var sillyFunction = function(recursed) {
    if (!recursed) { return arguments.callee(true); }
    if (this !== global) {
        alert('This is: ' + this);
    } else {
        alert('This is the global');
    }
}

sillyFunction();

ECMAScript 3 は、名前付き関数式を許可することでこれらの問題を解決しました。例えば:

[1, 2, 3, 4, 5].map(function factorial(n) {
    return !(n > 1) ? 1 : factorial(n - 1)*n;
});

これには多くの利点があります:

Another feature that was deprecated was arguments.callee.caller, or more specifically Function.caller. Why is this? Well, at any point in time you can find the deepest caller of any function on the stack, and as I said above looking at the call stack has one single major effect: it makes a large number of optimizations impossible, or much much more difficult. For example, if you cannot guarantee that a function f will not call an unknown function, it is not possible to inline f. Basically it means that any call site that may have been trivially inlinable accumulates a large number of guards:

function f(a, b, c, d, e) { return a ? b * c : d * e; }

If the JavaScript interpreter cannot guarantee that all the provided arguments are numbers at the point that the call is made, it needs to either insert checks for all the arguments before the inlined code, or it cannot inline the function. Now in this particular case a smart interpreter should be able to rearrange the checks to be more optimal and not check any values that would not be used. However in many cases that's just not possible and therefore it becomes impossible to inline.

例: 無名再帰関数内での arguments.callee の使用

再帰関数は自分自身を参照する必要があります。関数が自分自身を参照するには、一般的には関数の名前を使用します。しかしながら、無名関数には名前がありません。さらにその無名関数を参照するアクセス可能な変数も無い(関数がどの変数にも代入されていない)場合、その関数には自分自身を参照する手段がありません(無名関数は関数式または Function コンストラクターによって作成できます)。したがって、これを参照するアクセス可能な変数がない場合、関数が自分自身を参照できる唯一の方法は arguments.callee による方法です。

次の例では関数を定義し、その関数内でさらに階乗関数を定義し、それを返しています。

function create() {
   return function(n) {
      if (n <= 1)
         return 1;
      return n * arguments.callee(n - 1);
   };
}

var result = create()(5); // 120 (5 * 4 * 3 * 2 * 1) を返す

良い代替手段がない場合の arguments.callee の使用

ただし次のような場合、 arguments.callee に代わるものが無いために、その非推奨はバグである可能性があります ( {{Bug("725398")}} を参照):

function createPerson(sIdentity) {
    var oPerson = new Function('alert(arguments.callee.identity);');
    oPerson.identity = sIdentity;
    return oPerson;
}

var john = createPerson('John Smith');

john();

仕様策定状況

仕様書 状況 コメント
{{SpecName('ES1')}} {{Spec2('ES1')}} 初回定義。 JavaScript 1.2 で実装
{{SpecName('ES5.1', '#sec-10.6', 'Arguments Object')}} {{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-arguments-exotic-objects', 'Arguments Exotic Objects')}} {{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-arguments-exotic-objects', 'Arguments Exotic Objects')}} {{Spec2('ESDraft')}}

ブラウザー実装状況

{{Compat("javascript.functions.arguments.callee")}}

関連項目