I am trying to find out how to get this without invoking function or at least not invoking the original function. This requires expert understanding of javascript. It may not be even possible to begin with. But i think if it's it'd save lot of time writing by not using place holder functions like in events, callbacks, and setTimeouts...
This should refer to object the function belongs to with out directly invoking it.
The thing i wanted to do:
say i have function like this:
function add2(x) {
return x+2;
}
and then do this:
setTimeout(add2**pass parameter without invoking it**, 1000);
If i had done this
setTimeout(add2(3), 1000);
it would have executed right away.
so to do it other way, i do this:
Function.prototype.pass= function(){
var func=this, args=arguments;
return function() {
func.apply(this,args);
}
};
then call it like this:
setTimeout(add2.pass(3), 1000); //5
this is great and all and works. But this fails when it's inside a object and need to access object's data. The this in it always refer to function itself. So doing this would give an error:
var o= {
a:3,
log:function(){console.log(this.a);}
};
o.log.pass(); //will just output the log function itself.
i could have solved it only if could pass correct this
reference at line func.apply(this,args);
.