In JavaScript, if I have:
var foo = function(){} foo.prototype.bar = function() { console.log(this); } baz = new foo(); baz.name = 'baz'; bing = new foo(); bing.name = 'bing'; baz.bar(); bing.bar(); setTimeout(baz.bar); setTimeout(bing.bar);
In my console, I see baz , bing , window , window objects in that order.
What will be the simplest or “normal” trickster to see:
baz , bing , baz , bing written using the variable "this-like" inside the function bar on the prototype foo ?
When I say "this-like", I mean a variable that I could use in bar() to access the object, that it is a method both now and later.
Edit:
For further clarification, I would like to avoid the need to know when I call bar () that he wants to use this , and so I will need to do something like bind() . Suppose that when I call bar() on baz or bing, this is a black box. I want to call bar () as a bing or baz method, not knowing how this works, and I want the insides of bar () to know what the method is.
source share