...

View Full Version : Closure Issue



jaywhy13
05-06-2008, 08:34 PM
Have a look at this scriptlet...

var globalObj = new Object();

function testF(){
var someVariable = "helloWorld";
globalObj.f = function(){
alert("'" + someVariable + "'");
};
window.setTimeout("globalObj.f()",500);
someVariable = null;
}


I define a function on the fly that needs to take a value from the current closure in which it is defined.

On the page I have a link that calls the testF() function... it basically alerts 'null' (in quotes) ... I know the result will be null since when the function is called, it checks for the value and finds it to be null. Excluding the suggestion of not nulling the variable how can I possibly make the variable persist?

Trinithis
05-06-2008, 10:06 PM
Here's something that works as you wanted, but if I'm not mistaken, it defeats the purpose of nulling the initial variable. Have you considered having the callback (the function you send to setTimeout) null out the variable?


Function.prototype.bind = function(context) {
var m = this;
return function() {
return m.apply(context, arguments);
};
}

var globalObj = {};

function testF() {
var someVariable = "helloWorld";
var f = (function(someVariable) {
return function() {
alert("'" + someVariable + "'");
};
})(someVariable);
globalObj.f = f;
setTimeout(f.bind(globalObj), 500);
someVariable = null;
}

testF();

jaywhy13
05-07-2008, 10:19 PM
Here's something that works as you wanted, but if I'm not mistaken, it defeats the purpose of nulling the initial variable. Have you considered having the callback (the function you send to setTimeout) null out the variable?


Function.prototype.bind = function(context) {
var m = this;
return function() {
return m.apply(context, arguments);
};
}

var globalObj = {};

function testF() {
var someVariable = "helloWorld";
var f = (function(someVariable) {
return function() {
alert("'" + someVariable + "'");
};
})(someVariable);
globalObj.f = f;
setTimeout(f.bind(globalObj), 500);
someVariable = null;
}

testF();

Wow... well I understand it works... is there a good closure article you could recommend? I always get lost when I see this...


var f = (function(someVariable) {
.... })(someVariable);

Trinithis
05-07-2008, 10:24 PM
Try reading post #6 and #7 here: http://www.dynamicdrive.com/forums/showthread.php?t=28361

rnd me
05-08-2008, 06:48 AM
the variable IS being persisted, that why is says null, and not "helloworld"

rnd me
05-08-2008, 07:01 AM
the variable IS being persisted, thats why is says null, and not "helloworld"...

think of closure as a phone book listing not residents, but variables. the magic of javascript is that it provides a time-warping phone, such that a 'phone number' for john smith from 1995 can still be reach the john smith who lived there in 1995, at the 1995 phone number, even if his number has changed in 2008, even if he moved away, or frankly, even if he died.


you get a time-warp every time you wrap a function in a function. by controlling when (and also where) the functions fire, you can control the evaluation of the wrapped code . using various syntax you can grab a variable: locally inside the inner function, as a global, as a member-property of another object, or as closed outside variable as seen from inside during a prior evaluation cycle.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum