Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript - Get calling object [duplicate]

Tags:

javascript

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

Is there a way to get the value of this from the function which has called the current function?

Look at this:

function TraceMySelf(){
    console.log(this);
}
function A(){
    TraceMySelf();
    console.log(this);
}

var a = new A();

When this code is executed, the console displays first the window object and then the a object. How can I make the code display the a object twice, with only changing line 2? I know that I could apply the function inside A with this, but that isn't what I want.

Is this possible?

like image 313
Van Coding Avatar asked Mar 08 '11 21:03

Van Coding


1 Answers

I think this is the answer to your question: StackOverflow 280389

However, I think the right answer is "don't do that". I think it runs counter to how JavaScript is designed.

It might also be worth looking at jQuery Proxy for another way of linking function and object.

like image 127
JoshRivers Avatar answered Nov 09 '22 19:11

JoshRivers