Notes on JavaScript: The Good Parts
I recently bought a few books, and this “JavaScript: The Good Parts” truly lives up to its name. It’s short in length with extensive appendices. The author is Douglas Crockford, the inventor of JSON – sounds impressive, and indeed the book is packed with insights. I benefited greatly from reading it!
JavaScript: The Good Parts
Chapters 4 & 5
Functions & Inheritance
- If a function is invoked with the
newprefix, a new object is created behind the scenes, linked to the function’sprototypemember.
a. The linking is done via
__proto__, so the new object’s__proto__property has all the properties and methods fromprototype.b. Meanwhile,
thisis bound to this new object. If the return value is not an object,thisis returned instead.
1 | var that = Object.create(this.prototype); |
- The
newoperator creates a new object (inheriting from its operand’s prototype), then invokes the operand, bindingthisto the newly created object. This allows the operand (i.e., the constructor function) to customize the newly created object before returning it to the caller. - Due to the dynamic nature of JavaScript’s prototypal inheritance, new methods are immediately available to all instances of an object, even those created before the method was added to the corresponding class’s
prototypeproperty. - For
function A() {}, class A’s constructor resides on A’s prototype. Therefore, when inheriting using the following approach, the last step must reset the subclass’s constructor to itself:
1 | // Shape - superclass |
- About Object.create
The
**Object.create()**method creates a new object with the specified prototype object and properties.
1 | var o; |
Chapter 6
Arrays
- JavaScript arrays are objects with some array-like characteristics.
1 | var arr = []; |
- Setting a larger
lengthfor an array does not allocate more space, but settinglengthsmaller will delete all properties with indices greater than or equal to the newlength. - Since arrays are objects, you can use the
deleteoperator to remove elements.
1 | var arr = ['one', 'two', 'three']; |
- Since arrays are objects, you can add properties to them. When the property name is not a number, it does not increase
length.
Chapter 7
Regular Expressions
If you’re unsure whether a special character needs escaping, you can always add an escape character
\.\d [0-9] \D \s Unicode character \S \w [0-9A-Z_a-Z] \W . Any character except line terminators1
2
3
4
5
6
7
8
9
10
11
12
13
14
3. Regular expression grouping: `()`, regular expression character class: `[]`
4. Quantifiers:
```js
// If there is only one quantifier (of the form {1,}), greedy matching is performed
let reg = /w{3,}/;
'wwwww'.match(reg); // 'wwwww'
// Use ? for non-greedy matching
let reg2 = /w{3,}?/;
'wwwww'.match(reg2); // 'www'
Chapter 8
Methods
array.popandarray.shiftpop from different positions – one from the tail, the other from the head.array.pushandarray.unshiftpush to different positions – one to the tail, the other to the head.array.slice(start, end)performs a shallow copy;array.splice(start, deleteCount, item...)removes elements and replaces them with the provided items.regexp.exec(string),regexp.test(string)string.lastIndexOf(searchString, position),string.charAt(position),string.match(regexp),string.search(regex)
Appendix
1 + 0.2 // 0.30000000000000004 0.1 + 0.2 == 0.3 // false1
2
3
4
5
6
7
2. ```js
typeof NaN === 'number' // true
Nan === NaN // false
NaN !== NaN // true
isNaN(NaN) // true ES6 moved it to Number.isNaNChecking for arrays:
1 | arr && typeof arr === 'object' && arr.constructor === Array |
Enhanced version:
1 | Object.prototype.toString.apply(arr) === '[object Array]' |
- Bitwise operators are very slow in JavaScript.
- It is recommended to use
var foo = function foo() {};instead offunction foo() {};. - Avoid using type wrappers:
new Boolean(false),new Number(1),new String('abc'). - Avoid using
new Objectandnew Array. - In HTML, the literal
<character must be written as<.
JSON
- JSON has six value types: objects, arrays, strings, numbers, booleans, and null.
Notes on JavaScript: The Good Parts
http://quanru.github.io/2016/11/20/Notes-on-JavaScript-The-Good-Parts

