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:
|
I have two questions:
- Is
[[Environment]]
a Node.js thing and the equivalent of[[Scopes]]
on the front end? - Is this the best place to check for any closure data on a function?
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:
|
I have two questions:
- Is
[[Environment]]
a Node.js thing and the equivalent of[[Scopes]]
on the front end? - Is this the best place to check for any closure data on a function?
- 1 1) This might be browser specific. IE for example does not even show these scopes. But since this [[Scopes]] object in chrome fits the description for [[enviroment]], I would assume they're the same and [[Scopes]] is the Chrome implementation. 2) No idea, I've never been in a situation where checking the storage location of a closured variable of a function was important instead of checking the value of that closured variable. – Shilly Commented Aug 8, 2018 at 13:41
-
1
The
[[environment]]
as described in the spec text is a linked list. The[[scopes]]
in your screenshot looks more like an array. – Bergi Commented Aug 8, 2018 at 14:54 - As your book says, it's not a property. It's an internal slot (formerly known as internal property). You cannot access it, and you cannot "check for closure data" on a function. It's just a courtesy of your debugger to make it available for inspection. – Bergi Commented Aug 8, 2018 at 14:57
- @Bergi Where do you get the spec list? – VSO Commented Aug 8, 2018 at 15:22
- 1 @VSO ecma-international/ecma-262. I can't remend it for beginners, though – Bergi Commented Aug 8, 2018 at 15:43
2 Answers
Reset to default 5Looks 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.
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 pletely 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 ment.
I would remend these sites. Take a look. here1 here2