千天计划之第982天——四个变量名笔记
learn day by day
Values, Types, and Operators
http://eloquentjavascript.net/01_values.html
Below the surface of the machine, the program moves. Without effort, it expands and contracts. In great harmony, electrons scatter and regroup. The forms on the monitor are but ripples on the water. The essence stays invisibly below. Master Yuan-Ma, The Book of Programming
Inside the computer’s world, there is only data. You can read data, modify data, create new data—but anything that isn’t data simply does not exist. All this data is stored as long sequences of bits and is thus fundamentally alike.
机器之下,程序运行。屏幕上的内容只是水面的涟漪,本质存在不可见之底。
电脑的世界里,只有数据。
bit
比特,bits,常称为0,1,是两种对立的事物,可表示任何分离信息。
例如:13,表示为 0001101或者,8+4+1.
0 0 0 0 1 1 0 1
128 64 32 16 8 4 2 1
So that’s the binary number 00001101, or 8 + 4 + 1, which equals 13.
设想一个比特之海。
现代电脑就是这个样子的。
为了不迷失方向,你可以把这些比特所代表的信息分成各个小块。
在JavaScript环境中,这些就被称为values,各自代表不同角色。
其中有六种基本类型: numbers,strings,booleans, objects, functions, undefined values.
建立一个value(变量), 你需要唤起它,即命名一个变量。
不需集赞原材料,直接呼之即来,招之即走。
本章只讲一些简单的可操作变量。
numbers
十进制的数字的总数可表示为10的n次方,64位二进制的是2的64次方个数字,就是一个18后面18个0这样的数字。
小数用小数点表示。
arithmetic
数就是用来计算的,JavaScript 中就用+-/*运算。
%是求余,优先级和乘除一样。
special numbers
arithmetic
infinity和-infinity代表正负,infinity-1仍是正数。
NaN代表,not a number, 尽管这个是数的类型当你尝试除以零时就会得到这个结果。
strings
这个代表文本,text, 写作单/双引号之内。几乎任何东西都可以放进去,但有些比较难,比如引号放在引号里。换行符号也不能放进去。string只能保持一行。
引号中的/之后有不同意义,尽管还是引号内的一部分,比如引号中的/n代表引号中的换行,引号中的/t代表引号中的tab charater。
"This is the first line\nAnd this is the second"
The actual text contained is this:
This is the first line
And this is the second
strings不能被减和乘除运算,但可以加运算,即合并在一块儿。
unary operators
console.log代表输出,即看看我们代码运算的结果。
unary operators 指typeof,只占用一个字符的。
boolean values
代表true and false,即是和否,开和关等。
comparisons
Here is one way to produce Boolean values:
console.log(3 > 2)
// → true
console.log(3 < 2)
// → false
Strings can be compared in the same way.
console.log("Aardvark" < "Zoroaster")
// → true
另外还有,>=,<=,==,!=(大于,小于,等于,不等于)。
比如:
console.log("Itchy" != "Scratchy")
// → true
但是:
There is only one value in JavaScript that is not equal to itself, and that is NaN, which stands for “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.
logical operators
有三种:and, or, not.
&&代表and。
||代表or。
!代表not。
他们的优先级是,or最低, and其次,然后是大于小于号。
最后一个介绍的是,ternary, 写作?:
看例子:
The last logical operator I will discuss is not unary, not binary, but ternary, operating on three values. It is written with a question mark and a colon, like this:console.log(true ? 1 : 2);// → 1console.log(false ? 1 : 2);// → 2This one is called the conditional operator (or sometimes just ternary operator since it is the only such operator in the language). The value on the left of the question mark “picks” which of the other two values will come out. When it is true, the middle value is chosen, and when it is false, the value on the right comes out.
undefined values
null, undefined用作指代不存在的变量名,他们之间基本没区别。
automatic type conversion
操作者把不同类型变量混合使用,则Javascript自动偷偷转换它想要的变量类型,称之为type coercion。
===代表两边完全相等,这个时候就不会有自动转换类型的事情发生了。
short-circuiting of logical operations
arithmetic
看例子:
In the case of true || X, no matter what X is—even if it’s an expression 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.
summary
四种变量类型:numbers, strings, Booleans, undefined values
Such values are created by typing in their name (true, null) or value (13, "abc"). You can combine and transform values with operators. We saw binary operators for arithmetic (+, -, *, /, and %), string concatenation (+), comparison (==, !=, ===, !==, <, >, <=, >=), and logic (&&, ||), as well as several unary operators (- to negate a number, ! to negate logically, and typeof to find a value’s type) and a ternary operator (?:) to pick one of two values based on a third value.
由此,javascript可变成你便捷的计算器。当然远不止这些,下一章我们明天讲。
PDF:http://eloquentjavascript.net/Eloquent_JavaScript.pdf
pdf for phone: http://eloquentjavascript.net/Eloquent_JavaScript_small.pdf
点击阅读原文可看我在石墨文档编辑的笔记