Where does the browser store global variables defined with let or const?

If global variables defined with constant or let are not stored in the window, where are they stored?

var varTest = 'var test';
const constTest = 'const test';
let letTest = 'let test';

varTest            //"var test"
letTest            //"let test"
constTest          //"const test"
window.varTest     //"var test"
window.constTest   //undefined
window.letTest     //undefined

      

+3


source to share


1 answer


A global environment record has two parts

Environment records are conceptual data structures for storing mappings identifier name -> value

.

As you might suspect, object environment entries are backed by real user-space objects like a global object or arbitrary object when you use with

. What global bindings do become properties of the global object.



let

. const

, and other declarations are stored in a portion of the declarative environment record that is supported by some implementation-specific data structure. You have encountered declarative environments before, because every functional environment is a declarative environment. Therefore, you can also say that " let

and const

are saved in the global scope just like any binding is saved in a function."

From the specification:

A Global Environment Record is logically one record, but it is defined as a composite encapsulation of an Environment Record object and a declarative environment record. The Environment Record object has the global associated Realm object as its base object. This global object is the value returned by the specific GetThisBinding method of the global Recordset. The environment record component of the global environment record object contains bindings for all built-in global variables ( section 18) and any bindings introduced by FunctionDeclaration, GeneratorDeclaration, or VariableStatement contained in the global code. The bindings for all other ECMAScript declarations in the global code are contained in the declarative environment record component of the global environment record.

+4


source







All Articles