![]() ![]() For instance, consider the program: var a = 2. Tokenizing/Lexing: breaking up a string of characters into meaningful (to the language) chunks, called tokens. In classic compiler theory, a program is processed by a compiler in three basic stages: Scope is primarily determined during compilation, so understanding how compilation and execution relate is key in mastering scope. Compiling Codeīut first, why does it even matter whether JS is compiled or not? For the benefit of readers here, the following sections will revisit and expand on that assertion. Our conclusion there is that JS is most accurately portrayed as a compiled language. ![]() Recall that we surveyed this topic in Chapter 1 of the Get Started book. Modern JS engines actually employ numerous variations of both compilation and interpretation in the handling of JS programs. However, the issue is more nuanced, because interpretation can actually take other forms than just operating line by line on source code text. interpretation of programs.Īre these two processing models mutually exclusive? Generally, yes. Interpreted Codeįigure 1 illustrates compilation vs. 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. 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. 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. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |