你如何在JavaScript中找到调用函数? [英] How do you find out the caller function in JavaScript?

查看:184
本文介绍了你如何在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 at all?

推荐答案

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

请注意,此功能为非标准,来自 Function.caller

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


非标准

功能是非标准的,不符合标准。不要在面向Web的生产站点上使用它:它不适用于每个用户。实现之间可能存在很大的不兼容性,并且行为可能在将来发生变化。

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. There may also be large incompatibilities between implementations and the behavior may change in the future.






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


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

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

这篇关于你如何在JavaScript中找到调用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆