...

View Full Version : clear all



Belloshoes
09-30-2011, 09:02 AM
If I've declared some functions which include variables(e.g var k), what is the standard javascript code to clear/wipe the memory of these variables so that I may use the same variable names in later divs?

I know it is a weird question and you'd ask why don't you just use a different variable name.

Old Pedant
09-30-2011, 09:14 AM
Ummm...if the variables are declared (using var) *inside* of a function, then they automatically disappear when the function finishes. Presto. Gone. Memory and all.

Also, divs have nothing nothing nothing at all to do with JavaScript.

Finally, if the variable is a global (page scope) variable, then there's no need to 'clear' it before reusing it. Just assign a different value to it and the old value is gone. POOF.

jassi.singh
09-30-2011, 10:31 AM
Hi,
FYI
Their life is limited to the scope in which they are declared. for eg var x declared inside a function then it will automatically disappear or release as soon as the function gets over.

ironboy
10-01-2011, 04:16 PM
Actually, local variables are not "gone" after a function finishes - if there is something in the function that will form a closure - i.e. you define an inner function with a reference that survives the function scope - then they can be reached from this. (And the interpreter has to keep them in memory until the there is no reference to the closure...)


var closure;
var myFunc = function(){
var a=1, b=2, c=3;
closure = function(){alert(a+' '+b+' '+c)}
};

myFunc();
closure();

Old Pedant
10-02-2011, 12:00 AM
Ironboy is of course correct, but I think we can rate that as the exception rather than the rule. With most coding, you will never need or create closures. (When you do need to, though, they are most handy to have.)

ironboy
10-02-2011, 10:26 AM
Mm, I think it is a hard to avoid closures (even when you don't need them...). Consider:

var myFunc = function(els){
// A lot of vars this func needs
var a, b, c, d, i;
// attach some event
for(i = 0; i < els.length; i++){
els[i].onclick = function(){
// do something simple
}
}
};


No each of the functions bound to onclick events will create a closure that remembers els, a, b, c, d and i.

rnd me
10-02-2011, 11:00 AM
use pure functions (http://en.wikipedia.org/wiki/Pure_function) and you won't have to worry about performance-killing closures.

rnd me
10-02-2011, 11:02 AM
Mm, I think it is a hard to avoid closures (even when you don't need them...). Consider:

var myFunc = function(els){
// A lot of vars this func needs
var a, b, c, d, i;
// attach some event
for(i = 0; i < els.length; i++){
els[i].onclick = function(){
// do something simple
}
}
};


No each of the functions bound to onclick events will create a closure that remembers els, a, b, c, d and i.

no it won't. unless the event refers to an aforementioned variable, it won't be bundled on the activation object.
also, only those explicitly used in the event will be bundled.
since closures are slow, do your math "ahead of time" and close as little as possible; there's almost no reason an event needs 5-6 var refs like that.
using an extra anon-wrap (function(a,b,c){ /* bind events here */ }(a,b,c)); to reduce the scope of deep closures can help too, without having to rewrite event code.

ironboy
10-02-2011, 11:05 AM
@rnd me:
I don't understand half of what you just said there (try explaining it simpler),
but the fact remains - in the example I provided you create a bunch of closures. Period.

[EDITED FOR CLARITY:]
Also with "that this func needs" I didn't mean the function attached to the event - I meant the outer function (in which this comment resides) - and it's just a skeleton/example (could do have done a bunch of other things except assigning event handlers...)

rnd me
10-02-2011, 11:11 AM
@rnd me:
I don't understand half of what you just said there (try explaining it simpler),
but the fact remains - in the example I provided you create a bunch of closures. Period.

Also with "that this func needs" I didn't mean the function attached to the event - I meant the main function (elsewhere) - it's just a skeleton/example...

with JUST the code in your example, not a single closure is created... :P

what i'm saying is that not everything is always closed, which wasn't clear from your example or explanation.

cheers!

ironboy
10-02-2011, 11:14 AM
Proof of closures (even if you don't reference variables in the inner func):
var x;
(function(){
var a = 1, b = 2, c = 3
x = function(y){eval(y)};
})();
x("alert(a+' '+b+' '+c)");

rnd me
10-02-2011, 08:12 PM
Proof of closures (even if you don't reference variables in the inner func):

that's not the same as your previous example, but it shows why eval() can be harmful if not used correctly. it's the added activation overhead that keeps eval(exp) and with(obj) out of ES5 strict mode; js runs leaner and meaner without them...

ironboy
10-02-2011, 09:21 PM
@rnd_me: Seems I can't convince you of the nature of closures... I don't think they are evil or problematic in most cases, but they do exist to a far greater extent that you seem to think and the eval in the previous example didn't magically make the function act as a a closure - it would have even with an empty function...

If you don't believe me run any one of my examples (the first one with some inparameters - dom elements or empty objects to go into els) in Google Chrome and then ask the built in profiler for the amount of of memory that is taken up by closures... :cool:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum