getify / You-Dont-Know-JS

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

Confusion about Compiler and Scope Manager role #1695

Closed rohan-jha closed 3 years ago

rohan-jha commented 3 years ago

I already searched for this issue:

Edition: 2nd

Book Title: Scope & Closures

Chapter: Chapter 2: Illustrating Lexical Scope

Section Title: A Conversation Among Friends

Paragraph: Encountering var students, Compiler will ask Scope Manager to see if a variable named students already exists for that particular scope bucket. If so, Compiler would ignore this declaration and move on. Otherwise, Compiler will produce code that (at execution time) asks Scope Manager to create a new variable called students in that scope bucket.

Question: I have a confusion about the above paragraph. If the Compiler produces code that asks Scope Manager to create a new variable at execution time, then how Scope Manager knows about the variable when Compiler encounters var students, let's say for second time. This is my first deep dive into the language, pardon me if it is a trivial question.

rohan-jha commented 3 years ago

I have got the answer in subsequent section, closing the question. Excuse me for creating the issue, where it was not needed.