Reading <Up & Going>
Up & Going是You Don’t Know JS系列的第一本书,主要粗略的介绍了ES6的各个语言特性,可以说是从入门到放弃精通的很好读物。这里我摘了一些讲得不错或者我觉得比较有意思的段落,并在部分段落后面添加了当时读到时naive的想法。
The JavaScript engine actually compiles the program on the fly and then immediately runs the compiled code.
implicit coercion(隐式类型转换) is confusing and harms programs with unexpected bugs, and should thus be avoided. It’s even sometimes called a flaw in the design of the language.
There are lots of opinions on what makes well-commented code; we can’t really define absolute universal rules. But some observations and guidelines are quite useful:
- Code without comments is suboptimal.
- Too many comments (one per line, for example) is probably a sign of poorly written code.
- Comments should explain why, not what. They can optionally explain how if that’s particularly confusing.
In some programming languages, you declare a variable (container) to hold a specific type of value, such as
number
orstring
. Static typing, otherwise known as type enforcement, is typically cited as a benefit for program correctness by preventing unintended value conversions.Other languages emphasize types for values instead of variables. Weak typing, otherwise known as dynamic typing, allows a variable to hold any type of value at any time. It’s typically cited as a benefit for program flexibility by allowing a single variable to represent a value no matter what type form that value may take at any given moment in the program’s logic flow.
JavaScript uses the latter approach, dynamic typing, meaning variables can hold values of any type without any typeenforcement.
这段看得还是有点晕,其实说的是JavaScript是动态类型语言(虽然这里没提,它也是弱类型,作为对比,Python是强类型语言)。至于动态/静态,强/弱类型语言的区别,觉得轮子哥说的还比较清楚:
强类型:偏向于不容忍隐式类型转换。譬如说haskell的int就不能变成double
弱类型:偏向于容忍隐式类型转换。譬如说C语言的int可以变成double
静态类型:编译的时候就知道每一个变量的类型,因为类型错误而不能做的事情是语法错误。
动态类型:编译的时候不知道每一个变量的类型,因为类型错误而不能做的事情是运行时错误。譬如说你不能对一个数字a写a[10]当数组用。
JavaScript has typed values, not typed variables. The following built-in types are available:
string
number
boolean
null
andundefined
object
symbol
(new to ES6)
typeof null
is an interesting case, because it errantly returns"object"
, when you’d expect it to return"null"
. This is a long-standing bug in JS, but one that is likely never going to be fixed. Too much code on the Web relies on the bug and thus fixing it would cause a lot more bugs!
Properties can either be accessed with dot notation (i.e.,
obj.a
) or bracket notation (i.e.,obj["a"]
). Dot notation is shorter and generally easier to read, and is thus preferred when possible.Bracket notation is useful if you have a property name that has special characters in it, like
obj["hello world!”]
. Bracket notation is also useful if you want to access a property/key but the name is stored in another variable
You’ve probably heard sentiments like “coercion is evil” drawn from the fact that there are clearly places where coercion can produce some surprising results. Perhaps nothing evokes frustration from developers more than when the language surprises them.
Coercion is not evil, nor does it have to be surprising. In fact, the majority of cases you can construct with type coercion are quite sensible and understandable, and can even be used to improve the readability of your code.
“coercion is not evil”的情况,对于显式转换没什么问题,但对于隐式转换,我能想到的大概是一些常用的大家都知道的隐式转换,比如console.log()
这种(类似的有Python的string.format()和print(),当然Python这里不是coercion,而是调用的__str__方法,但效果和coercion接近),以及条件判断时也不必都转换为bool类型。其他情况我还是很赞同”(implicit) coercion is evil”的啊。