0

以下の問題で私を助けてください。

var test = new Object();
test.testInner = new Object();

test.testInner.main = function ()
{
   Hello();
}

function Hello()
{
  /**** Question: currently I am getting blank string with below code,
   **** Is there any way to get function name as "test.testInner.main" over here? */
  console.log(arguments.callee.caller.name);
}
test.testInner.main();
4

2 に答える 2

1

test.testInner.mainanonymous(名前なし) 関数の参照があります。それらに名前を割り当てることで、名前を取得できます。変更されたコード:

var test = new Object();
test.testInner = new Object();

test.testInner.main = function main()
{
   Hello();
}

function Hello()
{
  /**** Question: currently I am getting blank string with below code,
   **** Is there any way to get function name as "test.testInner.main" over here? */
  console.log(arguments.callee.caller.name);
}
test.testInner.main();

jsfiddle

于 2012-10-09T11:59:49.180 に答える
0

JavaScript で関数のコンテキストを設定できます。

function hello() { 
    console.log(this);
}
some.other.object = function() {
    hello.call(this, arguments, to, hello);
}

これは hello() の some.other.object になります。

あなたの例では、呼び出し元はメインであり、匿名であるため name プロパティがありません。ここのように:なぜarguments.callee.caller.nameが未定義なのですか?

また、引数は推奨されていないため、使用しないでください。

于 2012-10-09T12:00:34.063 に答える