The second pillar of the language is the prototypes system. To dig further into scope, closures, and how modules work, read Book 2, Scope & Closures.
YOU DONT KNOW JS PROTOTYPAL INHERITANCE CODE
As I see it, modules are as with the grain as you can get, when it comes to code organization in JS. When a function makes reference to variables from an outer scope, and that function is passed around as a value and executed in other scopes, it maintains access to its original scope variables this is closure.Īcross all of programming, but especially in JS, closure drives many of the most important programming patterns, including modules. All of these are just unique parts of the language that should be learned and understood by all JS developers.Ĭlosure is a natural result of lexical scope when the language has functions as first-class values, as JS does.
Though TDZ can be strange to encounter, it's also not an invalidation of lexical scoping. let/ const declarations have a peculiar error behavior called the "Temporal Dead Zone" (TDZ) which results in observable but unusable variables. Neither hoisting nor function-scoped var are sufficient to back the claim that JS is not lexically scoped. The other is that var-declared variables are function scoped, even if they appear inside a block. The first is commonly called hoisting: when all variables declared anywhere in a scope are treated as if they're declared at the beginning of the scope. JS is lexically scoped, though many claim it isn't, because of two particular characteristics of its model that are not present in other lexically scoped languages. In other words, it's an author-time decision: where you locate a function/scope in the program determines what the scope structure of that part of the program will be. The scope unit boundaries, and how variables are organized in them, is determined at the time the program is parsed (compiled).
This is how scopes behave in most languages, which is called lexical scope. Scopes nest inside each other, and for any given expression or statement, only variables at that level of scope nesting, or in higher/outer scopes, are accessible variables from lower/inner scopes are hidden and inaccessible. The scope model of a language is like the rules that help you determine which color marbles go in which matching-color buckets. Scopes are like buckets, and variables are like marbles you put into those buckets. The organization of variables into units of scope (functions, blocks) is one of the most foundational characteristics of any language perhaps no other characteristic has a greater impact on how programs behave. Also, don't skip the appendices, especially Appendix B, "Practice, Practice, Practice!". When you're ready, this final chapter divides the organization of the JS language into three main pillars, then offers a brief roadmap of what to expect from the rest of the book series, and how I suggest you proceed. Spend some more time looking through code in your current projects, and comparing what you see to what's been discussed so far. Rather than rushing onto the next book in an attempt to churn through all the books expediently, spend some time going back over the material in this book.
YOU DONT KNOW JS PROTOTYPAL INHERITANCE SERIES
The rest of the books in this series are where we will unpack all of the rest of the language, in far greater detail than we could have done in a few brief chapters here. I also hope that we've hinted at enough deeper detail throughout to pique your curiosity to want to dig more into the language. The goal is to fill in gaps that readers newer to JS might have tripped over in their early encounters with the language. This book surveys what you need to be aware of as you get started with JS. Armed with this knowledge, you can become a true JavaScript master.You Don't Know JS Yet: Get Started - 2nd Edition Chapter 4: The Bigger Picture Like other books in the "You Don't Know JS" series, this and Object Prototypes dives into trickier parts of the language that many JavaScript programmers simply avoid. You'll learn how they work and why they're integral to behavior delegation a design pattern in which objects are linked, rather than cloned. This concise, in-depth guide takes you inside JavaScript's this structure and object prototypes.
No matter how much experience you have with JavaScript, odds are you don't fully understand the language.