22

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?

peterh
  • 11,875
  • 18
  • 85
  • 108
Van Coding
  • 24,244
  • 24
  • 88
  • 132
  • i think the only way is to pass it as an argument – n00b Mar 08 '11 at 21:42
  • No way without passing it as argument? @hvgotcodes: In your metioned question, he asks for the calling FUNCTION ;) – Van Coding Mar 08 '11 at 21:44
  • 1
    @FlashFan: Only functions can call other functions. Objects can't. You cannot get the object a function is a property of because there exist no inverse relationship. @hvgotcodes: Yes it is a duplicate, but it should be noted that the usage of `arguments.callee` and such, is deprecated. – Felix Kling Mar 08 '11 at 21:47
  • And to be very exact: In your example, not `this` is calling `TraceMySelf`, but the constructor function `A`. – Felix Kling Mar 08 '11 at 21:51
  • @Felix: You're right, but if there would be a thing like an "object stack", where i could access the "this" values of the functions in the stack, it would be possible. But there's nothing like this, right? – Van Coding Mar 08 '11 at 21:52
  • @FlashFan: I see what you mean. Afaik there is nothing like that. At least nothing what you can access from JavaScript itself. – Felix Kling Mar 08 '11 at 21:53
  • 2
    @hvgotcodes There is a _DISTINCT_ difference in these questions – Raynos Mar 08 '11 at 21:53
  • whouldn't the prototypejs bind achieve this goal? var fnc =TraceMySelf; fnc.bind(this)(); – Liviu T. Mar 08 '11 at 21:54
  • @Felix: Thanks for your help. Then I must solve it in an other way. @ Liviu: bind would do the same as apply or call. And that's not what I want. – Van Coding Mar 08 '11 at 21:57
  • @LiviuT you mean the ES5 native `.bind` ;) @FlashFan what do you want to achieve _in_ context. We can suggest an alternative design that could emulate this for you – Raynos Mar 08 '11 at 21:57
  • @raynos, hmm it appears I cannot undo my close vote. Sorry. In any event, are you asking for the scope of the function that called the currently executing function? – hvgotcodes Mar 08 '11 at 21:59
  • @hvgotcodes @FlashFan more correctly he's asking for the `this` context rather then the `CallObject`. There is no way to get this through reflection you need to set up a different way of writing your code to achieve something similar. – Raynos Mar 08 '11 at 22:05
  • @Raynos - If this many people are getting the meaning of the question wrong, then maybe it's a problem with the question and not the people answering. Furthermore, you could have edited the question to make it more clear in less time than it's taken you to correct everyone one-by-one. – Wayne Mar 08 '11 at 23:02
  • @lwburk your right. I could have made the question more clear. I'll do that next time, Thanks. – Raynos Mar 08 '11 at 23:10
  • 2
    This is definitely not a duplicate of that question. I have this same question but could care less about the linked "duplicate". Vote for reopen. – devios1 Jun 01 '12 at 18:18
  • As a debugging hack, I've just created a debugging version of my function and did a search & replace of the code to use that version. E.g: function myFunc(param) = { ... } function myFuncDB(caller, param) = { console.debug(caller); return myFunc(param); } Then replace every occurrence of "myFunc(" with "myFuncDB(this,". – Waz Nov 25 '14 at 00:47

1 Answers1

2

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.

Community
  • 1
  • 1
JoshRivers
  • 9,920
  • 8
  • 39
  • 39
  • -1 Given the list of comments on the question, it has been mentioned that this is not the answer the user is looking for. – Raynos Mar 08 '11 at 22:43
  • 4
    You're right, there is discussion above. Most of it hidden behind a link. What isn't here is an answer. I don't think it's trolling to write one. Anyways. thanks for your feedback. – JoshRivers Mar 08 '11 at 22:59