inthenameofmusik
inthenameofmusik

Reputation: 623

Poorly Worded JavaScript Lexical Scope Example

I'm reading an article to try to understand lexical scope and how VariableEnvironment works. I understand the code and it makes sense but I don't get the preceding explanation and that worries me a bit. Could someone re-word it for me to make sure I'm completely understanding this?

Every function gets a [[scope]] property, and when the function is invoked the value of the scope property is assigned to the outer lexical environment reference (or outerLex) property of its VariableEnvironment. (ES 5 10.4.3.5-7) In this way, each VariableEnvironment inherits from the VariableEnvironment of its lexical parent. This scope chaining runs the length of the lexical hierarchy starting from the global object.

//VariableEnvironment: {x: undefined, etc.};
var x = "global";
//VariableEnvironment: {x: "global", etc.};

function outer() {
    //VariableEnvironment: {y: undefined, outerLex: {x: "global", etc.}};
    var y = "outer";    
    //VariableEnvironment: {y: "outer", outerLex: {x: "global", etc.}};

    function inner() {
        //VariableEnvironment: {x: undefined, outerLex: {y: "outer", outerLex: {x:"global", etc.}};
        var x = "inner";    
        //VariableEnvironment: {x: "inner", outerLex: {y: "outer", outerLex: {x:"global", etc.}};
    }
}

Is there also a way to access the VariableEnvironment of a function? function.VariableEnvironment? Or function.[[scope]]? Neither seem to work.


Update

The article in question is here

Upvotes: 1

Views: 64

Answers (2)

Dellirium
Dellirium

Reputation: 1516

This question largely relates, or rather is in fact a question about closures, as it is written in the article you linked, there is a really well written aticle about this, which helped me understand a lot, here it is:

About javascript closures

Upvotes: 1

Pointy
Pointy

Reputation: 413737

The first thing to understand is what that word lexical means in this context. The "lexical" scope is the scope that can be seen plainly by looking at the function in the context of its definition in the source code, at the declarations within the function (var, let, const, function), and at the declarations in the layers of enclosing functions outward to the global scope. It's a process of just finding the curly braces and surrounding function declarations or expressions.

That description, then, is just a formal way of saying that symbol references are resolved by looking for symbols defined in the local scope, and then in each of the succession of surrounding "more global" scopes, all the way out to the actual global scope. (Note that in Node, things are a little different, because the actual global scope is not directly part of the lexical scope chain.)

I've seen "lexical scoping" also referred to as "static scoping", I think, which makes sense as it implies that the scope rules are finalized at the point of creation for each function (and, in ES2015, each block, for let). In particular, scoping is not dynamic — there's nothing involved in the rules about where or when a function is invoked.

Upvotes: 1

Related Questions