Javascript 有没有办法在被调用者中获取调用者函数的名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5750895/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 18:41:29  来源:igfitidea点击:

Is there a way to get the name of the caller function within the callee?

javascriptjquery

提问by Jannis

Possible Duplicate:
Javascript how do you find the caller function?

可能的重复:
Javascript 如何找到调用函数?

I'm experimenting with javascript/jQuery a bit this morning and was trying to capture the caller name of the currently executing function.

今天早上我正在尝试使用 javascript/jQuery,并试图捕获当前正在执行的函数的调用者名称。

So in the below example, the log would show runMeas the caller and showMeas the callee.

所以在下面的例子中,日志将显示runMe为调用者和showMe被调用者。

jQuery(document).ready(function($) {

  function showMe() {
    // should log the runMe as the caller and showMe as callee
    console.log('Callee: ',arguments.callee)
    console.log('Caller: ',arguments.caller);
  }

  function runMe() {
    // getting executed as a button is pressed.
    showMe();
  }

  $('a').bind('click', function(e) {
    e.preventDefault();
    runMe();
  });

});

The above obviously doesn't work, hence my question to you all.

以上显然行不通,因此我向大家提问。

Is there a good way to get the caller in the above example?

在上面的例子中有没有一个好的方法来获取调用者?

please note: I am aware I could get the callee of runMeand pass it into showMeas an argument but this question is aiming towards a solution that does not require the caller to be passed into the function 'manually'.

请注意:我知道我可以得到被调用者runMe并将其showMe作为参数传递,但这个问题的目标是一个不需要调用者“手动”传递到函数的解决方案。

Are there reasons against doing something like this?

有什么理由反对这样做吗?

回答by Lightness Races in Orbit

You used to be able to do arguments.caller.name, but this is deprecated in Javascript 1.3.

您过去可以这样做arguments.caller.name,但在 Javascript 1.3 中已弃用

arguments.callee.caller.name(or just showMe.caller.name) is another way to go. This is non-standard, and not supported in strict mode, but otherwise currently supported in all major browsers (ref).

arguments.callee.caller.name(或只是showMe.caller.name)是另一种方式。这是非标准的,在严格模式下不受支持,但目前所有主要浏览器都支持(ref)。

回答by Satyajit

Try callee.callerlike this

callee.caller像这样尝试

 function showMe() {
        // should log the runMe as the caller and showMe as callee
        console.log('Callee: ',arguments.callee.name)
        console.log('Caller: ',arguments.callee.caller.name);
      }

回答by two7s_clash

Does this work for you?

这对你有用吗?

function showMe() {
    // should log the runMe as the caller and showMe as callee
    console.log('Callee: ',arguments.callee)
    console.log('Caller: ',arguments.callee.caller);
  }

Note, this is non-standard javascript.

请注意,这是非标准的 javascript。

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/caller

https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/caller

回答by T. Stone

I think it's....

我想这是....

arguments.callee.caller

arguments.callee.caller