I have a function which takes a callback function. How can I set the 'this' variable of the callback function?
eg.
function(fn){
//do some stuff
fn(); //call fn, but the 'this' var is set to window
//, how do I set it to something else
}
I have a function which takes a callback function. How can I set the 'this' variable of the callback function?
eg.
function(fn){
//do some stuff
fn(); //call fn, but the 'this' var is set to window
//, how do I set it to something else
}
you can execute a function in the context of an object using call:
fn.call( obj, 'param' )
There's also apply
Only difference is the syntax for feeding arguments.
funct.call(objThatWillBeThis, arg1, ..., argN);
or
funct.apply(objThatWillBeThis, arrayOfArgs);