This book is about ECMAScript 6 (whose official name is ECMAScript 2015), a new version of JavaScript.
In order to understand this book, you should already know JavaScript. If you don’t: my other book “Speaking JavaScript” is free online and teaches programmers all of JavaScript (up to and including ECMAScript 5).
This book covers ES6 with three levels of detail:
Other things to know:
I started writing this book long before there were implementations of ES6 features, which required quite a bit of research. Essential sources were:
ECMAScript 5 introduced language modes: Strict mode makes JavaScript a cleaner language by changing its semantics, performing more checks and throwing more exceptions. Consult Sect. “Strict Mode” in “Speaking JavaScript” for more information. The legacy/default mode is called non-strict mode or sloppy mode.
Strict mode is switched on via the following line (which does nothing in ECMAScript versions before ES5):
If you put this line at the beginning of a file, all code in it is in strict mode. If you make this line the first line of a function, only that function is in strict mode.
Using a directive to switch on strict mode is not very user friendly and was one of the reasons why strict mode was not nearly as popular in ES5 as it should be. However, ES6 modules and classes are implicitly in strict mode. Given that most ES6 code will live in modules, strict mode becomes the de-facto default for ES6.
The term protocol has various meanings in computing. In the context of programming languages and API design, I’m using it as follows:
A protocol defines interfaces (signatures for methods and/or functions) and rules for using them.
The idea is to specify how a service is to be performed. Then anyone can perform the service and anyone can request it and they are guaranteed to work together well.
Note that the definition given here is different from viewing a protocol as an interface (as, for example, Objective C does), because this definition includes rules.
Given a method call obj.m(···)
, obj
is the receiver of the method call and accessible via this
inside the method.
The (type) signature of a function describes how the function is to be called, what its inputs and its output are. I’m using the syntax established by Microsoft TypeScript and Facebook Flow in this book. An example of a signature:
You can see that parseInt()
expects a string and a number and returns a number. If the type of a parameter is clear, I often omit the type annotation.
The ES6 language specification uses internal slots to store internal data. In the spec, internal slots are accessed as if they were properties whose names are in square brackets:
Two things differentiate them from properties:
[[Prototype]]
. The value of that slot cannot be read directly via JavaScript, but you can use Object.getPrototypeOf()
to do so.How exactly internal slots are stored is left unspecified. Some may not even exist in actual JavaScript implementations.
The ECMAScript spec uses a data structure called environment to store the variables of a scope. An environment is basically a dictionary that maps variable names to values. A binding is an entry in an environment, storage space for a variable.
Destructive operations (methods, functions) modify their parameters or their receivers. For example, push()
modifies its receiver arr
:
In contrast, concat()
creates a new Array and does not change its receiver arr
:
The API of a class C
is usually documented as follows:
C
constructorC
methodsC.prototype
methodsIn English, I capitalize JavaScript terms as follows:
String
: its members are objects, instances of String
.string
: its members are primitive values, strings.map()
.Several repositories on GitHub contain code shown in this book:
async-examples
babel-on-node
demo_promise
generator-examples
node-es6-demo
promise-examples
webpack-es6-demo
Sidebars are boxes of text marked with icons. They complement the normal content.
Occasionally, I refer to (publicly available) external material via footnotes. Two sources are marked with a prefix in square brackets: