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.

5
  • Are you asking if variable hoisting could somehow interfere with how closures work? No, it can't. Commented Oct 4, 2013 at 5:30
  • i want to know my explanation is correct ? i know how closures work Commented Oct 4, 2013 at 5:32
  • It sounds like you understand it, but something is still not clear. I'm just not sure what. Commented Oct 4, 2013 at 5:47
  • can u just explain it in your own views Commented Oct 4, 2013 at 5:55
  • See also Programmatically accessing [[Scopes]], What is [[Scopes]] and How to get the scope from where a function was called? Commented Aug 16, 2021 at 8:40

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

3 Comments

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!
how does one access it?
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.