3

ok this is some code

function myFunc(){
   var myvar = 8;
       function myFunc2(num){
           alert(myvar+num);
       }

   myFunc2(2);

}

myFunc();

i want to clear my mind so pls correct me if am wrong

i have read allot of articles in stack overflow already but i want to know i understand it well or should i read more.

to my understanding what happens behind the scene is thatin global execution context there it creates function object with the namemyFunc` and its [[scope]] property assigned to global variable object.

and when i call myFunc it creates its own execution context and activation object where all of the function's arguments and function declaration is initialized before any line by line code execution.

when inner function object is created it's internal [[scope]] property is assigned the value of its outer execution context's variable object + global variable object so every function creates its own execution context but before that every function's internal [[scope]] property is assigned first.

i have read allot of articles in stack overflow already but i want to know i understand it well or should i read more.

abdul raziq
  • 849
  • 1
  • 7
  • 11
  • Are you asking if variable hoisting could somehow interfere with how closures work? No, it can't. – bfavaretto Oct 04 '13 at 05:30
  • i want to know my explanation is correct ? i know how closures work – abdul raziq Oct 04 '13 at 05:32
  • It sounds like you understand it, but something is still not clear. I'm just not sure *what*. – bfavaretto Oct 04 '13 at 05:47
  • can u just explain it in your own views – abdul raziq Oct 04 '13 at 05:55
  • See also [Programmatically accessing `[[Scopes]]`](https://stackoverflow.com/q/52150979/1048572), [What is `[[Scopes]]`](https://stackoverflow.com/q/45506745/1048572) and [How to get the scope from where a function was called?](https://stackoverflow.com/q/45313383/1048572) – Bergi Aug 16 '21 at 08:40

1 Answers1

1

Here are a couple of pointers based on my understanding of the specification, and based on what sounds unclear on your explanation:

  • The term "Activation object" was used in ECMAScript 3, but not anymore in the current version of the specification. ES5 uses the term "Lexical Environment" to denote a type (an internal type) consisting of an "Environment Record" value, and possibly a reference to an outer Lexical Environment.

  • Because of this reference to an outer Lexical Environment, scope can be thought of as a chain. So access to outer scopes (including the global scope) happens through that chain. (When you say that "[[scope]] property is assigned the value of its outer execution context's variable object + global variable object", it sounds like both records are copied into the current function's Lexical Environment, which is not how it happens.)

Hope this helps!

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Thank you! I was reading High Performance JS (©2010), and then the 2nd ed of Secrets of a JS Ninja (©2016) -- these ES3 terms disappeared in the latter and I had no clue why. Thanks! – Justin L. Nov 15 '16 at 03:40
  • 2
    how does one access it? – Edwin O. Jul 31 '19 at 10:02
  • @EdwinO. One does not. It's not an object, it cannot be accessed from code. What the debugger is showing you is just a representation of the engine internals. – Bergi Jul 29 '23 at 06:19