Share E-Book
Scan to open this page

Scan with your phone to open this page

Author: Kyle Simpson, 赵望野, 梁杰

JavaScript语言有很多复杂的概念,但却用简单的方式体现出来(比如回调函数),因此,JavaScript开发者无需理解语言内部的原理,就能编写出功能全面的程序;就像收音机一样,你无需理解里面的管子和线圈都是做什么用的,只要会操作收音机上的按键,就可以收听你喜欢的节目。然而,JavaScript的这些复杂精妙的概念才是语言的精髓,即使是经验丰富的JavaScript开发者,如果没有认真学习也无法真正理解语言本身的特性。正是因为绝大多数人不求甚解,一遇到出乎意料的行为就认为是语言本身有缺陷,进而把相关的特性加入黑名单,久而久之就排除了这门语言的多样性,人为地使它变得不完整、不安全。 “你不知道的JavaScript”系列就是要让不求甚解的JavaScript开发者迎难而上,深入语言内部,弄清楚JavaScript每一个零部件的用途。本书介绍了该系列的两个主题:“作用域和闭包”以及“this和对象原型”。掌握了这些知识之后,无论什么技术、框架和流行词语,你都能轻松理解。

AI Reading Assistant

Whole-book reading guide from stratified index samples; jump to passages in the text

AI guide
【One-Line Pitch】 A deep-dive into the two most misunderstood corners of JavaScript—scope/closures and `this`/prototypes—that turns "it just works" familiarity into genuine understanding. Best for developers who can ship code but freeze when asked *why* a callback sees the wrong value or why `this` isn't what they expected. 【Book Arc】 - **Opening (~0%–10%)**: Frames the mission—most developers blame the language for behavior they never learned, blacklisting features until only a "safe" subset remains. Establishes that JavaScript is a compiled language and that scope is the rule-set for storing and finding variables. - **Early (~10%–25%)**: Builds the scope foundation: LHS/RHS lookups, the nested "building/bubble" model of lexical scope, and the three scope units—function scope, block scope (`let`/`const`, `try/catch`), and IIFEs. Solves the question of *where variables live and how the engine finds them*. - **Early (~25%–35%)**: Covers hoisting (declarations rise, assignments don't; functions win over variables) and then closure—the payoff of lexical scope—leading into the module pattern and modern module loaders. - **Middle (~35%–55%)**: Shifts to `this`. Debunks the two big myths (that `this` points to the function itself or to its lexical scope), then introduces call-site analysis and the binding rules. - **Late (~55%–end)**: Moves into objects and prototypes—how `new` really differs from class-based languages, hard binding via `bind(..)`, and the "safer this" / DMZ-object technique. Excerpts thin out here, so later prototype-chain material is only lightly represented. 【Key Takeaways】 - **JavaScript is a compiled language, not purely interpreted** (Opening): The engine parses and compiles before executing, which is why hoisting and scope rules exist at all—understanding this reframes "weird" behavior as predictable. - **Scope is a lookup system, and LHS vs. RHS matters** (Early): Assignments (LHS) find a *container*; reads (RHS) find a *value*. Distinguishing them explains reference errors and silent global leaks. - **Lexical scope is fixed at author time** (Early): Nested scopes behave like floors in a building—lookups climb outward and stop at the global scope. `eval` and `with` can cheat this, but doing so is a documented anti-pattern. - **Block scope is real and useful beyond `var`** (Early): `let`/`const` and `try/catch` give finer-grained scope, which also helps garbage collection by not retaining large data longer than needed. - **Hoisting is function-first** (Early): Function declarations are hoisted before variable declarations, so a later `var foo` won't override an earlier `function foo`—a classic source of confusion. - **Closures are the natural consequence of lexical scope** (Early): Once you understand scope, closures stop being magic; the module pattern is just closure applied deliberately, including the ability to mutate a public API from inside. - **`this` is determined by the call site, not where the function is written** (Middle): Four binding rules apply in priority order, and reading the call stack (or a debugger breakpoint) is the reliable way to find the real call location. - **Arrow functions abandon `this` binding entirely** (Middle): They inherit `this` lexically, which is why they "fix" the `var self = this` problem—but also why they behave unlike normal functions. 【Reading Tips】 - **Deep-read the scope chapters (Opening–Early)**: They are the prerequisite for everything else; skimming here makes closures and `this` feel arbitrary. - **Skim the acknowledgments and front matter**: The excerpted material includes long contributor lists and publishing details that carry no technical content. - **Treat `this` as a decision tree, not a vibe**: When confused, locate the call site first, then apply the binding rules in order—this is the book's core method. - **Run the examples yourself**: Many snippets target ES6-era engines; testing them (and their pre-ES6 polyfill equivalents) cements the mental model. - **Take away the "why," not the syntax**: The book's value is a durable mental model that outlives any framework. 【Coverage Limits】 The excerpts cover scope, closures, hoisting, and the early `this`/binding material well, but the later object/prototype chapters are only lightly represented, so this guide cannot detail the full prototype chain or `new`-vs-class mechanics.
Page 8
.................................................................................28 3.4 块作用域 ...................................................................
View in text
Excerpt 2
..) 中所执行的代码包含有一个或多个声明(无论是变量还是函 数),就会对 eval(..) 所处的词法作用域进行修改。技术上,通过一些技巧(已经超出我 们的讨论范围)可以间接调用 eval(..) 来使其运行在全局作用域中,并对全局作用域进行 修改。但无论何种情况,eval(..) 都可以在运行期修改书写期的词...
View in text
Excerpt 3
var something = "cool"; 注 6: Bob Barker 是美国著名的电视节目主持人。——译者注 作用域闭包 | 51 foo1.identify(); // "foo 1" foo2.identify(); // "foo 2" 模块模式另一个简单但强大的变化用法是,命名将要作为公共 AP...
View in text
Excerpt 4
JavaScript 中的所有函数都 是对象),那就可以在调用函数时存储状态(属性的值)。这是可行的,有些时候也确实有 用,但是在本书即将介绍的许多模式中你会发现,除了函数对象还有许多更合适存储状态 的地方。 不过现在我们先来分析一下这个模式,让大家看到 this 并不像我们所想的那样指向函数 本身。 我们想要记...
View in text
Excerpt 5
neProperty( myObject, "FAVORITE_NUMBER", { value: 42, writable: false, configurable: false 2. 禁止扩展 如果你想禁止一个对象添加新属性并且保留已有属性,可以使用 Object.prevent Extensions(..)...
View in text
Excerpt 6
作推翻)了动态脚本中对应的语义。 “继承”这个词会让人产生非常强的心理预期(参见第 4 章)。仅仅在前面加上“原型”并 不能区分出 JavaScript 中和类继承几乎完全相反的行为,因此在过去 20 年中造成了极大的 误解。 在我看来,在“继承”前面加上“原型”对于事实的曲解就好像一只手拿橘子一只手拿苹 果然后...
View in text
Excerpt 7
我们会看到为什么解决语法上的问题无法真正解 除对于 JavaScript 中类的误解,尽管它看起来非常像一种解决办法! 无论你使用的是传统的原型语法还是 ES6 中的新语法糖,你仍然需要用“类”的概念来对 问题(UI 控件)进行建模。就像前几章试图证明的一样,这种做法会为你带来新的麻烦。 6.2.2 委托控件对象...
View in text
Excerpt 8
变)。 class 似乎不赞成这样做,所以强制让你使用丑陋的 .prototype 语法以及 super 问题,等 等。而且对于这种动态产生的问题,class 基本上都没有提供解决方案。 换句话说,class 似乎想告诉你:“动态太难实现了,所以这可能不是个好主意。这里有一 种看起来像静态的语法,所以编写静态代码...
View in text
Tags
AI categories
Programming LanguageJavaScriptWeb Technology
javascript
ISBN: 7115385734
Publish Year: 2015
Language: Chinese
File Format: PDF
File Size: 16.3 MB
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Generating text preview…