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 6 of 6

Thread: Closure Issue

  1. #1
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Closure Issue

    Have a look at this scriptlet...
    Code:
    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?
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #2
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    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?

    Code:
    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();
    Trinithis

  • #3
    Regular Coder
    Join Date
    Dec 2004
    Location
    Jamaica
    Posts
    592
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Trinithis View Post
    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?

    Code:
    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...

    Code:
    var f = (function(someVariable) {
       ....  })(someVariable);
    I'm gonna find a way to download the internet if its the last thing I do...
    Prepare to bow down to me (or my grave) and call me almighty when the algorithm is finished

  • #4
    Regular Coder
    Join Date
    Jun 2007
    Location
    USA
    Posts
    527
    Thanks
    26
    Thanked 74 Times in 72 Posts
    Trinithis

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,344
    Thanks
    11
    Thanked 589 Times in 570 Posts
    the variable IS being persisted, that why is says null, and not "helloworld"
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,344
    Thanks
    11
    Thanked 589 Times in 570 Posts
    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.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    Posting Permissions

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