Eloquent JavaScript
Notes
Chapter 1. Values, Types and Operators
JavaScript stores Number
type as 64 bits.
JavaScript has three “special” numbers, Infinity
-
Infinityand
NaN`.
JavaScript uses the Unicode standard for strings, but it uses the 16 bits version which can complicate things that are outside the 16 bit range such as emojis taking two “character positions”.
JavaScript uses null
and undefined
as special values to denote “emptiness”.
For coalescing the main difference between using ||
vs ??
is ||
tests if the left value is “truthy” so if the value is 0
it will be false is choose the default. Often times this is not what we want and instead should use ??
which checks if the value is null
or undefined
.
Chapter 6. The Secret Life of Objects
I love the example of explaining polymorphism using the a for/of
loop and an iterable. Clearly demonstrates how polymorphic code can operate on any type as long as it conforms to a specific “interface”. For for/of
loop will work on any iterable which just means it has a method named Symbol.iterator
which when called should return an object with a second interface called iterator which has a next
method and the result should have a value
property otherwise a done
property if there is no next.
Here is an example of implement a link list with this interface
class List {
constructor(value, rest) {
this.value = value;
this.rest = rest;
}
get length() {
return 1 + (this.rest ? this.rest.length : 0);
}
static fromArray(arr) {
let result = null;
for (let i = arr.length - 1; i >=0; i--) {
result = new this(arr[i], result);
}
return result;
}
}
class ListIterator {
constructor(list) {
this.list = list;
}
next() {
if (this.list == null) {
return {done: true};
}
let value = this.list.value;
this.list = this.list.rest;
return {value, done: false}
}
}
List.prototype[Symbol.iterator] = function() {
return new ListIterator(this);
};
let list = List.fromArray([1, 2, 3]);
for (let element of list) {
console.log(element);
}
Review
I thought it was a great introduction to JavaScript. I really enjoyed the exercises and how all the code examples were runnable when reading the version on the website. I skimmed all chapters that covered JavaScript in the browser as my main goal is to learn how to use it on the server.
I also found out the Author is the creator of CodeMirror which I used in SQL Translate!