Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
472 views
in Technique[技术] by (71.8m points)

javascript - Is there an es6 module-scope equivalent to `window`?

In Javascript, we can add global variables at any point using the window object:

'use strict';
var a = 1;

function test() {
  window.b = 2;
}

test();
console.log(a); // a
console.log(b); // b
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Module environments consist of so called declarative environment records:

A module Environment Record is a declarative Environment Record that is used to represent the outer scope of an ECMAScript Module.

In contrast to the global environment consist of an object environment record:

Each object Environment Record is associated with an object called its binding object. An object Environment Record binds the set of string identifier names that directly correspond to the property names of its binding object.


Just like with function environments (they are also declarative), there is no way to access the scope from another scope.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...