7

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?
VSO
  • 11,546
  • 25
  • 99
  • 187
  • 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 Aug 08 '18 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 Aug 08 '18 at 14:54
  • As your book says, it's not a property. It's an [internal slot](https://stackoverflow.com/q/50449953/1048572) (formerly known as [internal property](https://stackoverflow.com/q/11003021/1048572)). 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 Aug 08 '18 at 14:57
  • @Bergi Where do you get the spec list? – VSO Aug 08 '18 at 15:22
  • 1
    @VSO http://ecma-international.org/ecma-262/. I can't recommend it for beginners, though – Bergi Aug 08 '18 at 15:43
  • @Bergi Thanks, also, sick burn bro. – VSO Aug 08 '18 at 15:54

2 Answers2

5

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.
user3468806
  • 313
  • 4
  • 11
1

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

redchicken
  • 311
  • 1
  • 5
  • 18