Eloquent JavaScript #
When action grows unprofitable, gather information; when information grows unprofitable, sleep. Ursula K. Le Guin, The Left Hand of Darkness
索引
-
作者的幾個觀點
-
ECMAScript standard
- Values, Types, and Operators
-
Web browsers are not the only platforms on which JavaScript is used. Some databases, such as MongoDB and CouchDB
這個教程應該是在持續更新的,作者在 Introduction 中表示書中用的是 2017 版本的 JavaScript
COPY ↓
Keeping programs under control is the main problem of programming.
The art of programming is the skill of controlling complexity.
Some programmers believe that this complexity is best managed by using only a small set of well-understood techniques in their programs. They have composed strict rules (“best practices”) prescribing the form programs should have, and carefully stay within their safe little zone.
This is not only boring, it is also ineffective. New problems often require new solutions. The field of programming is young and still developing rapidly, and is varied enough to have room for wildly different approaches. There are many terrible mistakes to make in program design, and you should go ahead and make them so that you understand them. A sense of what a good program looks like is developed in practice
Standard ECMA-262 - Ecma International
After its adoption outside of Netscape, a standard document was written to describe the way the JavaScript language should work, so that the various pieces of software that claimed to support JavaScript were actually talking about the same language. This is called the ECMAScript standard, after the Ecma International organization that did the standardization.
Values, Types, and Operators
- Bits
-
Values -
每個值都必須存儲在某個地方,如果您想同時使用大量數據,則可能會導致內存不足。幸運的是,只有在同時需要大量值的時候,這才是一個問題。只要你不再使用值,它就會消失,留下它的一部分 作為下一代值的建築材料。
本章介紹JavaScript程序的原子元素,即簡單的值類型和可以處理這些值的操作符。
-
Numbers - JavaScript 用定長的比特存儲數字 -- -JavaScript uses a fixed number of bits, namely 64 of them, to store a single number value. you can represent 264different numbers, which is about 18 quintillion (an 18 with 18 zeros after it). 僅僅是 2 的 64 次方個不同數字!並不是指如此多的正整數。 ---- - Not all whole numbers below 18 quintillion fit in a JavaScript number, though. Those bits also store negative numbers, so one bit indicates the sign of the number. A bigger issue is that nonwhole numbers must also be represented. To do this, some of the bits are used to store the position of the decimal point. The actual maximum whole number that can be stored is more in the range of 9 quadrillion (15 zeros)—which is still pleasantly huge.
-
Special numbers -- The first two are
Infinity
and-Infinity
, which represent the positive and negative infinities.Infinity - 1
is stillInfinity
, and so on. Don’t put too much trust in infinity-based computation, though. It isn’t mathematically sound, and it will quickly lead to our next special number:NaN
. ---------------NaN
stands for “not a number”, even though it is a value of the number type. You’ll get this result when you, for example, try to calculate0 / 0
(zero divided by zero),Infinity - Infinity
, or any number of other numeric operations that don’t yield a meaningful result. - Strings, too, have to be modeled as a series of bits to be able to exist inside the computer. The way JavaScript does this is based on the Unicode standard. This standard assigns a number to virtually every character you would ever need, including characters from Greek, Arabic, Japanese, Armenian, and so on. If we have a number for every character, a string can be described by a sequence of numbers. -- -‘/n‘ -- - And that’s what JavaScript does. But there’s a complication: JavaScript’s representation uses 16 bits per string element, which can describe up to 216different characters. But Unicode defines more characters than that—about twice as many, at this point. So some characters, such as many emoji, take up two “character positions” in JavaScript strings.
-
Unary operators -- --Not all operators are symbols. Some are written as words. One example is the
typeof
operator, which produces a string value naming the type of the value you give it. 這裏提到一個非符號一元操作符 typeof → console.log(typeof 4.5) output = number -
Comparison - - -There is only one value in JavaScript that is not equal to itself, and that is
NaN
(“not a number”). console.log(NaN == NaN) // → false - ----- -NaN
is supposed to denote the result of a nonsensical computation, and as such, it isn’t equal to the result of any other nonsensical computations. -
Empty values -- 別把它和 Special numbers 搞混了,它已經脫離了 數(numbers)的範疇了。
有兩個特殊值,寫成 null 和 undefined,用來表示意義缺失的值(也就是沒有意義的值)。 它們本身是值,但它們沒有包含任何信息。
語言中有許多沒有得到有意義的值的操作(稍後會看到)會產生 undefined,因為它們必須產生一些值。↑ (雖然 沒有包含任何信息)
undefined 和 null 之間含義的差異 是 JavaScript設計的一個意外,大多數時候它並不重要。 在你不得不關註這些值的情況下,我建議將它們視為大體上可互換的。
Empty values # 摘
There are two special values, written null
and undefined
, that are used to denote the absence of a meaningful value. They are themselves values, but they carry no information.
Many operations in the language that don’t produce a meaningful value (you’ll see some later) yield undefined
simply because they have to yield some value.
The difference in meaning between undefined
and null
is an accident of JavaScript’s design, and it doesn’t matter most of the time. In the cases where you actually have to concern yourself with these values, I recommend treating them as mostly interchangeable.
Automatic type conversion # 摘
rue. When you do not want any automatic type conversions to happen, there are two additional operators: ===
and !==
. The first tests whether a value is precisely equal to the other, and the second tests whether it is not precisely equal. So "" === false
is false as expected.
I recommend using the three-character comparison operators defensively to prevent unexpected type conversions from tripping you up. But when you’re certain the types on both sides will be the same, there is no problem with using the shorter operators.
We can use this functionality as a way to fall back on a default value. If you have a value that might be empty, you can put ||
after it with a replacement value. If the initial value can be converted to false, you’ll get the replacement instead. 邏輯運算的奇淫巧技
Another important property of these two operators is that the part to their right is evaluated only when necessary. In the case of true || X
, no matter what X
is—even if it’s a piece of program that does something terrible—the result will be true, and X
is never evaluated. The same goes for false && X
, which is false and will ignore X
. This is called short-circuit evaluation.
The conditional operator works in a similar way. Of the second and third value, only the one that is selected is evaluated.
Eloquent JavaScript #