admin管理员组

文章数量:1403516

I have

 var X={
     method1 : function(){
                A();
              },
     method2 : function(){
                A();
              },
    
    }

function A(){
       console.log('im from methodNAME of ObjNAME ');
}

How can I find out the name of the method and Object that the function is called from?

I have

 var X={
     method1 : function(){
                A();
              },
     method2 : function(){
                A();
              },
    
    }

function A(){
       console.log('im from methodNAME of ObjNAME ');
}

How can I find out the name of the method and Object that the function is called from?

Share Improve this question edited Jan 16, 2023 at 19:50 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Nov 4, 2011 at 9:40 SarathSarath 9,15612 gold badges54 silver badges86 bronze badges 5
  • 1 Isnt it an option to just pass it as an argument to the function? – TJHeuvel Commented Nov 4, 2011 at 9:46
  • Two problems here: Objects don't have names. The name of the variable it is assigned to is not an object's name, you won't be able to find it (unless the variable is global). What if an object is assigned to multiple variables? Similar for the functions in your example. Although functions can have names and often have, yours do not, they are anonymous. – Felix Kling Commented Nov 4, 2011 at 9:52
  • Felix Kling : generally is there any way to find the name of a global Object , in my case X is global.. – Sarath Commented Nov 4, 2011 at 9:56
  • Only if you have a reference to the object. Then you can iterate over the properties of window, pare their values and if you have a match, you have the name of the variable. But note that if you have to do this, your application is not well designed. You should not have global variables anyway. – Felix Kling Commented Nov 4, 2011 at 10:14
  • Developing the global objects iteration idea further... If you have a reference to the caller method (e.g. via A.caller), you can recursively iterate over all properties of all global objects and check if their properties are this caller. – katspaugh Commented Nov 4, 2011 at 11:07
Add a ment  | 

3 Answers 3

Reset to default 5

You can use arguments.caller but it's not reliable: https://developer.mozilla/en/JavaScript/Reference/Functions_and_function_scope/arguments/caller

It has been replaced with https://developer.mozilla/en/JavaScript/Reference/Global_Objects/Function/caller which seems to have reasonable browser support.

You can use console.log(new Error()) within the function and parse the list of stack trace.

The way you defined and called your A function the object that A is called for will be DOMWindow object - that is, the global object.

As for from which method of X it is called (which is what you mean by methodNAME, I assume) - in the way you defined your methods (defining an anonymous function and assigning ir to a property) you won't be able to get the name.

Had you declared your X object like this:

var X = {
    method1: function method1() {
        A();
    },
    method2: function method2() {
        A();
    },

}

and your A function like this:

function A() {
    console.log(A.caller);
}

then calling:

X.method1();
X.method2();

would produce console output like this:

function method1() {
        A();
    }

function method2() {
        A();
    }

which you could then parse and retrieve the name of the calling method.

Or if you defined A like this:

function A() {
    console.log(A.caller.prototype);
}

then the output on would show:

method1
method2

where method1 and method2 are prototype objects - so you'd have to perform some manipulations as well.

EDIT: fixed bad copy/paste = A.caller.prototype -> A.caller.

本文标签: How do you find out the caller Object in JavaScriptStack Overflow