Javascript the Good Parts

Chaper 1: Good Parts

JavaScript has more in common with Lisp and Scheme than it does with Java. It is Lisp in C’s clothing.

The bad ideas include: a programming model based on global variables. JavaScript depends on global variables for linkage.

The good ideas include: functions, loose typing, dynamic objects, and an expressive object literal notation. You can write better programs by relying exclusively on the good parts. The DOM is quite awful though JavaScript is unfairly blamed. Strong typing does not eliminate the need for careful testing and they’re not the errors that Crockford worries about as a developer. He finds loose typing to be liberating.

Prototypal Inheritance: JavaScript has a class-free object system in which objects inherit properties directly from other objects. If you attempt to apply classical design patterns directly to JavaScript, you will be frustrated.

Chaper 2: Grammar

The block comments form, /* */, came from a language called PL/I Block comments are not safe for commenting out blocks of code.

There are certain words reserved by the ECMA Script Spec. The list includes words that are not used. In addition, some words should have been reserved but were not.

Numbers: JavaScript has a single number type. Internally, it is a 64-bit floating point number. 1 and 1.0 are the same value. Overflow in short integers are completely avoided. NaN is not equal to any value, including itself. Infinity represents all values greater than the max positive or less than the negative number.

Strings: \ backslash is the escape character. All characters in JavaScript are 16 bits wide. \u allows for referencing unicode character points numerically. Strings have a .length property. Strings are immutable.

Statements: switch, while, for, do, if, return, break, and throw. A block is a set of statements wrapped in curly braces.

Blocks in Javascript do not create a new scope. Functions create a new scope. Function scope NOT block scope.

Falsy values: false, null, undefined, '', 0, NaN

Operator precedence

+ adds or concatenates

Chapter 3: Objects

Simple types of Javascript are numbers, strings, booleans, null, undefined. All other values are objects. Numbers, strings, and booleans are object like but they are immutable. Arrays, functions, regex are objects. Properties can have any value except undefined. Objects are passed by reference.

Prototype linkage feature that allows an object to inherit the properties of another.

The logical OR operator (||) can be used to fill in object default values: let status = flight.status || 'unkown';

Trying to retrieve values from an undefined value will throw a TypeError exception. This can be gaurded against by using the logical and && operator: &&

Object Literals and Prototypes

Object literals are one way to instantiate an object. All objects created from object literals are linked to Object.prototype. When you make a new object you can select the object that should be its prototype:

let myBaseObject = {a: 57};
let myObj = function () {};
myObj.prototype = myBaseObject;
let instantiated = new myObj();
console.log(instantiated); // instantiated.a equals 57
instantiated.a = 34;
console.log(instantiated); // instantiated.a equals 34