【问题标题】:how to know which method is calling other method in javascript [duplicate]如何知道哪个方法在javascript中调用其他方法[重复]
【发布时间】:2013-09-11 08:11:19
【问题描述】:

考虑一下我在 jscript 中有很多方法.. 方法1()

方法2()

方法3()

现在 method1() 和 method2() 是独立的。其中 method3() 由两种方法调用。我想知道从哪个方法 method3() 被调用。 method1() 或 method2()

【问题讨论】:

  • @luiges90 嗯,是的,它看起来是重复的。但我找不到它并感谢您的帮助:)

标签: javascript jscript


【解决方案1】:

这里是简单的代码

function method1(){
  method3('method1');
}

function method2(){
  method3('method2');
}

function method3(method){
  alert(method);
}

Reference

【讨论】:

  • 谢谢,这很有帮助
【解决方案2】:

试试这个。

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

【讨论】:

  • 它也会在警报中显示方法主体,例如调用者是 function met1() { method3(); }
  • 它说包 arguments.callee 不存在
【解决方案3】:

这是记录调用者方法名称的示例代码:

function method1(){
  method3();
}

function method2(){
  method3();
}

function method3(){
    console.log(method3.caller.name);
}
method1(); // method1
method2(); // method2

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多