Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the Javascript [[Environment]] Property?

I am reading Secrets of the Javascript Ninja and am trying to figure out where the closure variables of a function are stored.[[Environment]] property available on the function identifier:

Whenever a function is created, a reference to the lexical environment in which the function was created is stored in an internal (meaning that you cannot access or manipulate it directly) property named [[Environment]] (this is the notation that we’ll use to mark these internal properties). In our case, the skulk function will keep a reference to the global environment, and the report function to the skulk environment.

All I see on my function is [[Scopes]], which contains the closure scope:

| enter image description here

I have two questions:

  1. Is [[Environment]] a Node.js thing and the equivalent of [[Scopes]] on the front end?
  2. Is this the best place to check for any closure data on a function?
like image 392
VSO Avatar asked Aug 08 '18 13:08

VSO


2 Answers

Looks like [[Scope]] is an old name for [[Environment]]; here

Set F.[[Environment]] to Scope.

While ES5 docs call it [[Scope]]; here

Set the [[Scope]] internal property of F to the value of Scope.
like image 185
user3468806 Avatar answered Oct 07 '22 20:10

user3468806


1 : Is [[Environment]] a Node.js thing and the equivalent of [[Scopes]] on the front end?

Um ... I think the question is wrong. Because both are created in the phase of creation the execution context.

And I think [[Environment]] and [[Scope]] are completely different.

[[Scope]] contains a list of variables that can be accessed within a certain scope, which allows you to search for (scope chain) variables.

[[Environment]] knows the lexical environment. In addition, this is my idea (don't believe it too much because it might be wrong), because there's an Environment, I think you can use closures. Because it refers to an external lexical environment.

2 : Is this the best place to check for any closure data on a function?

Yes. As I said in step 1, I think it's appropriate to check the closure data for a function because it refers to an external lexical environment that is larger than itself.

I hope my opinion helps you a lot. If this is wrong, please leave a comment.

I would recommend these sites. Take a look. here1 here2

like image 26
redchicken Avatar answered Oct 07 '22 21:10

redchicken