Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
157 views
in Technique[技术] by (71.8m points)

javascript - 如何找到JavaScript中的调用者函数?(How do you find out the caller function in JavaScript?)

function main()
{
   Hello();
}

function Hello()
{
  // How do you find out the caller function is 'main'?
}

Is there a way to find out the call stack?

(有没有办法找出调用堆栈?)

  ask by Ray Lu translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
function Hello()
{
    alert("caller is " + Hello.caller);
}

Note that this feature is non-standard , from Function.caller :

(请注意,此功能是Function.caller 的非标准 Function.caller :)

Non-standard

(非标)
This feature is non-standard and is not on a standards track.

(此功能是非标准的,不在标准轨道上。)

Do not use it on production sites facing the Web: it will not work for every user.

(请勿在面向Web的生产站点上使用它:它不适用于每个用户。)

There may also be large incompatibilities between implementations and the behavior may change in the future.

(实现之间也可能存在很大的不兼容性,并且将来的行为可能会更改。)


The following is the old answer from 2008, which is no longer supported in modern Javascript:

(以下是2008年的旧答案,现代Javascript不再支持该答案:)

function Hello()
{
    alert("caller is " + arguments.callee.caller.toString());
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...