interpretation of programs.Īre these two processing models mutually exclusive? Generally, yes.
Unlike a program being compiled all at once, with interpretation the source code is transformed line by line each line or statement is executed before immediately proceeding to processing the next line of the source code.įigure 1 illustrates compilation vs. Interpretation performs a similar task to compilation, in that it transforms your program into machine-understandable instructions. You also may have heard that code can be interpreted, so how is that different from being compiled? Typically, the whole source code is transformed at once, and those resulting instructions are saved as output (usually in a file) that can later be executed. Code compilation is a set of steps that process the text of your code and turn it into a list of instructions the computer can understand. You may have heard of code compilation before, but perhaps it seems like a mysterious black box where source code slides in one end and executable programs pop out the other. Modules are a code organization pattern characterized by public methods that have privileged access (via closure) to hidden variables and functions in the internal scope of the module. But since these functions hold and access variables, they maintain their original scope no matter where in the program the functions are eventually executed. JS functions are themselves first-class values they can be assigned and passed around just like numbers or strings.
The resulting scope structure is generally unaffected by runtime conditions. The code author's decisions on where to place variables, functions, and blocks with respect to each other are analyzed according to the rules of scope, during the initial parsing/compilation phase. But JS is in fact parsed/compiled in a separate phase before execution begins. JS is typically classified as an interpreted scripting language, so it's assumed by most that JS programs are processed in a single, top-down pass. Our focus will be the first of three pillars in the JS language: the scope system and its function closures, as well as the power of the module design pattern. Welcome to book 2 in the You Don't Know JS Yet series! If you already finished Get Started (the first book), you're in the right spot! If not, before you proceed I encourage you to start there for the best foundation. Our first step is to uncover how the JS engine processes our program before it runs. This book will dig through all aspects of scope-how it works, what it's useful for, gotchas to avoid-and then point toward common scope patterns that guide the structure of programs. The answers to questions like these take the form of well-defined rules called scope. I don't mean how the memory is allocated on the computer, but rather: how does JS know which variables are accessible by any given statement, and how does it handle two variables of the same name? Working with variables is one of the most foundational things we do in programming!īut you may not have considered very closely the underlying mechanisms used by the engine to organize and manage these variables. Armed with this knowledge, you can achieve true JavaScript mastery.You Don't Know JS Yet: Scope & Closures - 2nd Edition Chapter 1: What's the Scope?īy the time you've written your first few programs, you're likely getting somewhat comfortable with creating variables and storing values in them.
Like other books in this series, You Don’t Know JS: ES6 & Beyond dives into trickier parts of the language that many JavaScript programmers either avoid or know nothing about. As part of the "You Don’t Know JS" series, this compact guide focuses on new features available in ECMAScript 6 (ES6), the latest version of the standard upon which JavaScript is built. No matter how much experience you have with JavaScript, odds are you don’t fully understand the language.