I have something similar to this
function testfunction(runthis)
{
runthis();
}
function main()
{
var z = 15;
testfunction(function(){ alert(z); });
}
However it doesn’t think z is in the same scope as my inline function. Without adding additional parameters to testfunction or my inline function, is there any way for my inline function to belong to the same scope as main? I’m trying to make callback functions work.
Edit: the above I imagine is a crappy example because it seems to be working. However the instance here http://pastebin.com/A1pq8dJR does not work unless I add .call(this,parameters) and manually set the scope (although I’m not exactly sure what this is doing). I would have thought this used there would refer to the scope imageLoaded has, but it is referring to the scope of imageBoxCreate? Could anyone explain why it wont work without that and why doing this fixed it?
If you just invoke a global function in javascript, the
thisobject will be the top levelwindowglobal object. If you invoke a function using the dot operator on an object, thenthiswill be that object when the function runs. When you usefunction.call, you are explicitly specifying which object should bethis. I think you are likely just making some scope mistakes with how you usethisandvar, but your code is long enough and involved enough that I’m not going to spend the time to debug it for you. If you can isolate you issue with a smaller code sample, folks should be able to help more easily.