![lexical scope you dont know js lexical scope you dont know js](https://res.cloudinary.com/practicaldev/image/fetch/s--ozzZVOQb--/c_imagga_scale,f_auto,fl_progressive,h_1080,q_auto,w_1080/https://dev-to-uploads.s3.amazonaws.com/i/ydhjjic0lyjwnowmtb7t.png)
I don't recommend this book for beginners, not for intermediate, not for anyone. Really? lol) Part of me believes that maybe the book's purpose is to turn off as many people from programming as possible in order to keep the supply of devs low. How the fuck does this happen? You have to be a special kind of bad teacher to be able to pull this off. I WAS READING ABOUT STUFF I ALREADY KNEW VERY WELL. Turns out I felt pretty lost by the end of ch. From about half-way through page 1 onwards, all I could think was, "if I didn't already know this stuff, I would be so lost and confused that I would give up the idea of programming forever". There is not one single thing he writes about that I didn't already know, and yet, the order in which he presents new concepts and their explanations are so confused, convoluted and muddled that I actually feel dumber after reading it. 3, I stopped reading in angry frustration.
![lexical scope you dont know js lexical scope you dont know js](https://img1.daumcdn.net/thumb/R1280x0/?scode=mtistory2&fname=https:%2F%2Fblog.kakaocdn.net%2Fdn%2FdkjQ11%2FbtqJjHDgoNj%2Fhi3DbGfjZsWT474juKuDuk%2Fimg.png)
![lexical scope you dont know js lexical scope you dont know js](https://i.gr-assets.com/images/S/compressed.photo.goodreads.com/books/1403170838i/20901022._UY630_SR1200,630_.jpg)
Maybe it's a good time to go back and try again, I thought. By the end of the 3rd chapter, I was so lost I gave up.įast forward several months to the point where I know considerably more JS than I did on my first attempt at the book. I saw EloJS being proselytized everywhere so much that I thought if I read it I might actually find God. A year ago I decided to learn JS seriously and in-depth in order to make it my career. I have a very solid grasp of all the fundamentals such as type, variables, loops, functions, etc. I've been hobby programming on occasion ever since I was a teen. If you write a book for beginners that is too complex for beginners, you done fucked up. Why is this a problem? Because it's a book that was written FOR beginners. This is used to create a module in JS.I'm sorry, but someone's got to say it. The closure is the concept that a function declared in the lexical scope keeps a reference to it’s enclosing scope even executed outside of its original lexical scope. By contrast, “RHS” look-up returns ReferenceError. Without Strict mode, “LHS” look-up creates a new variable in the global scope when the variable is not found. “LHS” look-up is used to assign a variable, and “RHS” look-up is used to retrieve a value in the variable. When the Engine looks up the variable in the scope, it operates either “LHS” look-up or “RHS” look-up. The function declaration takes more priority than the variable declaration. Therefore, it doesn’t result ReferenceError to use a variable declared in the later code. This step is what’s called Hoisting in JS. When JS Engine executes JS program, first the compiler adds a variable to the scope. This chapter was about scoping and closure. The reason why you can call doSomething and doAnother in the instance of CoolModule is because doSomething and doAnother has a closure to the lexical scope of CoolModule. This is a common pattern you see on the JS code. If “Strict Mode” is enabled, this behavior is not allowed and it raises ReferenceError as well. By contrast, if “LHS” look-up reaches the global level without finding it, and if the program is not running in “Strict Mode”, the global Scope will create a new variable in the global scope and keeps running. When “RHS” could not find a variable, it raises a ReferenceError.
![lexical scope you dont know js lexical scope you dont know js](https://cdn.slidesharecdn.com/ss_thumbnails/you-dont-know-js-scope-and-closures-you-dont-know-js-2-191112041728-thumbnail-4.jpg)
The reason why we distinguish “LHS” and “RHS” is because it behaves differently when the Scope doesn’t have a variable. When the variable is not found in the current scope, it goes up till the global Scope. “RHS” look-up is trying to retrieve the value assigned to the variable. “LHS” look-up is trying to find where to store a value. Depending on the purpose, the Engine executes two types of variable look-up: “LHS” lookup or “RHS” lookup. When the engine looks up a variable, it’s interested in either what’s stored in the variable or where to assign a value. When the Engine doesn’t find a, it raises an error.
LEXICAL SCOPE YOU DONT KNOW JS CODE
When the Engine actually runs a code produced by the Compiler, it first checks where a is stored in the Scope, then assign it. Compiler then generates a code to assign a to be 2.Otherwise, Compiler asks Scope to create a new variable a Encountering var a, Compiler asks Scope to check if a exists.This type of language is called “Interpreter” language.įor example, when JS Engine compiles a statement var a = 2 It does two things. Each note is summarized per each chapter. Here are notes I took while studying famous notebook to understand Javascript You Don’t Know JS: Scope & Closure.