1

I couldn't help noticing the analogy between JS objects and lexical environments (as explained, for example, here). Both are containers of name/value pairs. Both have a link to another thing of the same kind: in the case of lexical environments, the parent environment, and in the case of JS objects, the prototype object. Both kinds of links practically serve the same purpose: in the case of lexical environments, to look up a variable's value in the chain of lexical environments, and in the case of objects, to look up a property's value in the prototype chain.

In view of this close analogy, is there a way to reference a lexical environment as an object from within a JS program? Are there any plans to add such a feature to the language in the future? I'm sure it's useful for something...

Ron Inbar
  • 2,044
  • 1
  • 16
  • 26
  • 4
    No, it is forbidden by ECMA-262. A lexical environment is a specification device to describe behaviour, it doesn't have to actually exist as described: "*Lexical Environments and Environment Record values are purely specification mechanisms and need not correspond to any specific artefact of an ECMAScript implementation. It is impossible for an ECMAScript program to directly access or manipulate such values.*" – RobG Dec 13 '18 at 20:26
  • As _I'm sure it's useful for something..._ isn't really a question for SO :-) – Randy Casburn Dec 13 '18 at 20:31
  • 1
    the opposite of `with`? AFAIK, the closest is shorthand literals, like `var scope = {a,b,c, scope};` – dandavis Dec 13 '18 at 21:01
  • @dandavis No, that's not what I meant. See https://mitpress.mit.edu/sites/default/files/sicp/full-text/book/book-Z-H-21.html#%25_sec_3.2 for an explanation of lexical environments. – Ron Inbar Dec 13 '18 at 22:41
  • Funnily I just spent a good few hours combing through the specification to build a proposal, so it's pretty fresh in my mind. In short, No, the underlying mechanics don't cross over from parsing to allow that to happen, and hopefully never will. If you're interested in the implementation of ECMAScript go ahead and take a look at the spec https://tc39.github.io/ecma262/ - specifically I believe `binding` should be of interest to you. – zfrisch Dec 13 '18 at 22:51
  • @zfrisch That's the *specification*, not the *implementation*. – Bergi Dec 13 '18 at 22:57
  • 1
    Closely related: [Are JavaScript execution contexts and JavaScript objects the same thing from a logical point of view?](https://stackoverflow.com/a/48418592/1048572) – Bergi Dec 13 '18 at 22:58

1 Answers1

1

There is one important distinction where the analogy breaks down: lexical environments are created from static code analysis, and do not change their shape. They are records, not dynamic objects. This both helps the interpreter/compiler to optimise lookups, and prevents them from being exposed as objects.

There are only two little things that introduce dynamic scoping: eval and with. (The latter allows to actually put dynamic objects in the scope chain). They're both despised because of this.

Bergi
  • 630,263
  • 148
  • 957
  • 1,375