Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How is block scope managed in the lexical environment?

If I declare a variable in a function using var then a slot for that variable is added to the LexicalEnvironment defined by that function.

function() {
  var foo; 
}

In the above code the LexicalEnvironment associated with the function contains a slot with a key foo and a value of undefined.

If I use a block-scoped declaration, how is the surrounding LexicalEnvironment affected?

function() {
  {
    let foo; // How does this affect the LexicalEnvironment?
  }
}
like image 969
Ben Aston Avatar asked Apr 02 '15 10:04

Ben Aston


People also ask

Is lexical environment the same as scope?

Lexical Environment is the environment of the function where it is written. That is, the static order/place where it is situated, regardless from where it is called from. Scope of a variable/function is basically the locations from where a variable is visible/accessible.

What does it mean to be block scoped?

Block scoped variables: A block scoped variable means that the variable defined within a block will not be accessible from outside the block. A block can reside inside a function, and a block scoped variable will not be available outside the block even if the block is inside a function.

What is a lexical environment?

A lexical environment is a data structure that holds identifier-variable mapping. (here identifier refers to the name of variables/functions, and the variable is the reference to actual object [including function object or primitive value]. Lexical in general means in hierarchy or in a sequence.

What is the lexical scope?

Lexical scoping, also known as static scoping, is a convention used with many modern programming languages. It refers to setting the scope, or range of functionality, of a variable so that it may be called (referenced) from within the block of code in which it is defined.


1 Answers

function() {
  var foo; 
}

As you mentioned, foo is available in the LexicalEnvironment is global to all inner functions within that function.

But

function() {
  {
    let foo; // How does this affect the LexicalEnviroinment?
  }
}

Here foo is local to that block alone. It won't be visible outside that block.

How does it affect the LexicalEnvironment ?

If you are referencing, foo anywhere inside that block, the local let foo will override the global var foo which you've defined in that function.

With respect to ES6,

function example(x) {
    console.log(y); // ReferenceError: y is not defined
    if (x) {
        let y = 5;
    }
}

Variables declared with a let statement are created as bindings on the lexical environment, rather than the variable environment, of the current execution context. A change to the specification of block statements in ES6 means that each block has its own lexical environment. In the above example, a new lexical environment is created when the block (the body of the if statement) is evaluated. When the let statement is evaluated a binding is added to this lexical environment and is innaccessible from the outer lexical environment (that of the function declaration itself).

Refer

like image 127
mohamedrias Avatar answered Oct 07 '22 18:10

mohamedrias