Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 13 of 13

Thread: clear all

  1. #1
    New Coder
    Join Date
    Aug 2011
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    clear all

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,128
    Thanks
    80
    Thanked 4,556 Times in 4,520 Posts
    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.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Regular Coder
    Join Date
    Sep 2011
    Posts
    103
    Thanks
    0
    Thanked 14 Times in 14 Posts
    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.

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    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...)

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

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,128
    Thanks
    80
    Thanked 4,556 Times in 4,520 Posts
    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.)
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts

    Post

    Mm, I think it is a hard to avoid closures (even when you don't need them...). Consider:
    Code:
    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.
    Last edited by ironboy; 10-02-2011 at 10:36 AM.

  • #7
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    use pure functions and you won't have to worry about performance-killing closures.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by ironboy View Post
    Mm, I think it is a hard to avoid closures (even when you don't need them...). Consider:
    Code:
    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.
    Last edited by rnd me; 10-02-2011 at 11:08 AM.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #9
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    @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...)
    Last edited by ironboy; 10-02-2011 at 03:15 PM.

  • #10
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by ironboy View Post
    @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!
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #11
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    Proof of closures (even if you don't reference variables in the inner func):
    Code:
    var x;
    (function(){
      var a = 1, b = 2, c = 3
      x = function(y){eval(y)};
    })();
    x("alert(a+' '+b+' '+c)");

  • #12
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,444
    Thanks
    11
    Thanked 598 Times in 578 Posts
    Quote Originally Posted by ironboy View Post
    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...
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #13
    Regular Coder
    Join Date
    Sep 2011
    Location
    Sweden
    Posts
    154
    Thanks
    1
    Thanked 22 Times in 22 Posts
    @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...


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •