Показаны сообщения с ярлыком scope. Показать все сообщения
Показаны сообщения с ярлыком scope. Показать все сообщения

пятница, 16 декабря 2016 г.

Closure and LexicalEnvironment

       
  • Each variable on create get [[Scope]] - link on object in context where it was created
  •    
  • [[Scope]] - is private internal property of function
  •    
  • On function call: create a new object with LexicalEnvironment's (LE) variables. This object get link on external object from [[Scope]]
  •    
  • Searching variables: at first it search in current variable's object and then by link [[Scope]]

Example


function saySomething(text) {
   // LexicalEnvironment = {sign: undefined}
   var sign = '!!!';
   // LexicalEnvironment = {sign: '!!!'}
   console.log('I say ' + text);

   function getText(text) { // [[Scope]] -> {sign: '!!!'}
      return text;
   }
}
saySomething('Hello World');

Where:
saySomething.[[Scope]] = window
getText.[[Scope]] = variable's object of current saySomething() call.

Scope


Initialization order

  1. Initialization
    1. Function Declaration on first step 
    2. var (adds to window global object: var a -> window.a) on second step 
  2. Assignment
!NB: Further comments in block of codes are states of initialization


Example 1

(function() {
   // window: {a: undefined, fo: undefined}
   var a = 5;

   // window: {a: 5, fo: undefined}
   var fo = function f() {
      // window: {a: undefined}
      console.log(a);
      var a = 1;
   }
   fo();
})();

Example 2

(function() {
   // window: {a: undefined, fo: undefined}
   var a = 5;

   // window: {a: 5, fo: undefined}
   var fo = function f() {
      // window: {a: 5} // 'a' has alredy exists
      console.log(a);
      a = 1;
   }
   fo();
})();

Example 3

(function() {
   // window: {a: undefined, fo: undefined}
   var a = 5;

   // window: {a: 5, fo: undefined}
   var fo = function f() {
      // window: {a: undefined, b: undefined, f2: function, e: error}
       console.log('a ' + a);
       console.log('b ' + b);
       console.log('e ' + e);
       var a = 1;
       var b = 2;
       e = 0;

       function f2() {
           console.log('f2 ' + f2);
       }
       f2();
   };
   fo();
})();