I have a function:
myObject.myFunction = function(callback){
callback();
}
and a callback
randomObject.callBack = function(){
console.log(this);
}
if I call randomObject.callBack()
directly, I get the parent object in the console. However, if I call myObject.myFunction(randomObject.callBack)
, it logs a DOM Element.
How can I access the parent object?
Note
I do not know the name of the callbacks parent object ahead of runtime.
I have a function:
myObject.myFunction = function(callback){
callback();
}
and a callback
randomObject.callBack = function(){
console.log(this);
}
if I call randomObject.callBack()
directly, I get the parent object in the console. However, if I call myObject.myFunction(randomObject.callBack)
, it logs a DOM Element.
How can I access the parent object?
Note
I do not know the name of the callbacks parent object ahead of runtime.
Share Improve this question edited Jan 11, 2012 at 13:53 Mild Fuzz asked Jan 11, 2012 at 13:13 Mild FuzzMild Fuzz 30.9k34 gold badges105 silver badges152 bronze badges3 Answers
Reset to default 5The context (i.e. this
value) of an object is determined at the time the function is run, not at the time it is defined. Passing randomObject.callBack
to another function does not send the context (the randomObject
bit); it just sends the function.
Presumably the context is set when myFunction
calls it. Since you aren't explicitly providing a context (e.g. with call
or apply
), the context will be the window
object.
You can change this by explicitly saying what the context of the function should be before it is run. You can do this with the bind
method:
myObject.myFunction(randomObject.callBack.bind(randomObject))
Now when you call callback
inside myFunction
, randomObject
will be logged.
Note that bind
is relatively new; not all browsers support it. The MDN page I linked to above has a bit of code that will make it work in all browsers.
This happens because when you invoke a function without an object, inside the function this
will point to Window object.To avoid this we usually do like this
myObject.myFunction = function(callback){
callback();
}
randomObject.callBack = function(){
console.log(this);
}
function proxyCallback(){
randomObject.callBack();
}
myObject.myFunction(proxyCallback);
In javascript, this
refers to the object context in which a function is called. This is not necessarily related to any object on which it has been defined.
You can think of it as though functions are not defined as members of objects, but called as members of objects.
There are four things that this
might resolve to, depending on context.
- A newly created object, if the function call was preceded by the new keyword.
- The Object to the left of the dot when the function was called.
- The Global Object (typically window), if neither of the above are provided.
- The first argument provided to a
call
orapply
function.
In your situation, something like this might be appropriate:
myObject.myFunction(function(){randomObject.callBack()});
This creates a closure so that within myFunction
, callback
is called as a member of randomObject
.