Is it possible to find which method was called on an object without having it in body of the object?
I mean :
function foo() {
if(! (this instanceof foo) ) return new foo();
alert(this.find_which_method_was_called()); // output 'myMethod'
}
foo().myMethod();
myMethod()is called after thefoo()constructor returns, so there is no chance you could know if it’s called or not in the constructor.You could, however, wrap your object in a proxy and save the name of all called functions in an array:
Now you can do this: