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.
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Yes. It's a good video.
    However, I had already read this, this and this, so nothing in the video was new to me.

    I think this is just a wording thing. I understand the concepts. I know which value is being used for variables and the this keyword in all the examples I've read.

    I just can't marry it up with the bold text in my original post. The variable scope at the time of invocation is the scope at the time of definition. Just because there is a variable which seems, at a first glance to be in scope, does not make it in scope at the time of invocation.

    In summary, I understand. I just don't really follow the choice of words by the author. In my mind, there is no distinction between when they were defined and when they were invoked.

  2. #17
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by onefootswill View Post
    In my mind, there is no distinction between when they were defined and when they were invoked.
    Code:
    var i=0;
    var say=(function scope(i){  //create new lexical scope using inline self-executing function
    
     // i is a var that should exist during the inline function's invocation
     
      return function say(){ //return the util method to the say var assignment;
         alert(i);
      }
    
      }(i)); //end scope()
    
    
    
    
    //now try to get say() to show anything besides 0, adjust snything you want  before you invoke say();
    //if they are the same, you should be able to get say() to say whatever you want by setting i before you invoke say()...
    alert( "source of say(): \n\n" + say);
    say(); i=88; say()
    Last edited by rnd me; 11-05-2013 at 04:15 AM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  3. Users who have thanked rnd me for this post:

    onefootswill (11-05-2013)

  4. #18
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK. I think I comprehend. I mean, I totally understand the closure.

    And for the language aspect, your (well made) point is that i has the value of 88 when say() is invoked the 2nd time.

    So, the scope of say when it was defined closes over the value of i that is passed in when the interpreter invoked the anonymous function. And that is the time of definition of say.

    i is 88 at the time say is invoked.

    How'd I go?
    Last edited by onefootswill; 11-05-2013 at 05:21 AM. Reason: Clarity

  5. #19
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by onefootswill View Post
    OK. I think I comprehend. I mean, I totally understand the closure.

    And for the language aspect, your (well made) point is that i has the value of 88 when say() is invoked the 2nd time.

    So, the scope of say when it was defined closes over the value of i that is passed in when the interpreter invoked the anonymous function. And that is the time of definition of say.

    i is 88 at the time say is invoked.

    How'd I go?
    yup. scope goes from most specific to least specific, so the inner "i" conceals the outer i.

    this let's us do tricks like:

    Code:
    function castratedEval(strCode){
       "use strict";
       var window, document, XMLHttpRequest, alert;
      return eval(strCode);
    }
    
    castratedEval("alert(34)")
    //TypeError: undefined is not a function
    castratedEval("confirm(34)"); // shows "34"
    which prevents some arbitrary code from seeing many attack vectors. now don't think that's enough by any means, i'm just showing how localization of lexical scope provides concealment and closure provides privacy.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  6. #20
    New to the CF scene
    Join Date
    Sep 2013
    Posts
    9
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Cool. Thanks very much for your help.

    The important part for me was connecting in my mind the statement in my first post to how the closures work via lexical scoping. The journey has got me there


 
Page 2 of 2 FirstFirst 12

Posting Permissions

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