getify / You-Dont-Know-JS

A book series on JavaScript. @YDKJS on twitter.
Other
177.97k stars 33.41k forks source link

Chapter 2: Illustrating Lexical Scope - Confusion about Conversation Among Friends #1706

Closed divnych closed 3 years ago

divnych commented 3 years ago

Yes, I promise I've read the Contributions Guidelines (please feel free to remove this line).


Please type "I already searched for this issue": I already searched for this issue.

Edition: 2nd

Book Title: Scope & Closures

Chapter: Chapter 2: Illustrating Lexical Scope

Section Title: A Conversation Among Friends

Question: I really liked a conversation metaphor used for describing how scopes work. But I got a bit confused after reading the second conversation. It seems to me like the dialogue lacks a couple of replicas to wholly describe what's happening. So I'll just quote the dialogue itself and leave my comment in the place that had me stumped.

Here it is:

Later, when it comes to execution of the program, the conversation will shift to Engine and Scope Manager, and might play out like this:

Engine: Hey, Scope Manager (of the global scope), before we begin, can you look up the identifier getStudentName so I can assign this function to it?

(Global) Scope Manager: Yep, here's the variable.

Engine: Hey, Scope Manager, I found a target reference for students, ever heard of it?

(Global) Scope Manager: Yes, it was formally declared for this scope, so here it is.

Engine: Thanks, I'm initializing students to undefined, so it's ready to use.

Hey, Scope Manager (of the global scope), I found a target reference for nextStudent, ever heard of it?

(Global) Scope Manager: Yes, it was formally declared for this scope, so here it is.

Engine: Thanks, I'm initializing nextStudent to undefined, so it's ready to use.

Hey, Scope Manager (of the global scope), I found a source reference for getStudentName, ever heard of it?

(Global) Scope Manager: Yes, it was formally declared for this scope. Here it is.

Should not now Engine assign the array to students variable first before it is going to execute getStudentName what will occur on the next line?

Engine: Great, the value in getStudentName is a function, so I'm going to execute it.

Engine: Hey, Scope Manager, now we need to instantiate the function's scope.

...

Also, in the last paragraph of the section, where you summarize the statement var students = [ .. ] you say:

  1. While Engine is executing, to process the assignment part of the statement, Engine asks Scope Manager to look up the variable, initializes it to undefined so it's ready to use, and then assigns the array value to it.

Should not this assignment be reflected in the dialogue at the place I've pointed out? Please excuse me, if that's a stupid question.

getify commented 3 years ago

The conversation focuses on the operations (both compile-time and run-time) that represent interactions between Engine and Scope Manager, not all operations in the code. The assignment (by Engine) of the array value indeed happens, but it's not particularly relevant to Scope Manager, so Engine doesn't mention it in the conversation. The note made at the end is a summary of order of operations, not just a recap of the conversation, so it includes additional relevant details.

divnych commented 3 years ago

Thank you very much for that explanation. Now it makes sense for me.