JavaScript: The Good Parts

Just finished reading this book for the second time. It’s usually the first book developers read when coming to JavaScript, and I admit I didn’t really understand all of it back then. Reading it again with more experience was useful, as it allowed better understanding, questioning of “why” on the points that are made, and realisations of where some of the things mentioned could be useful.

JavaScript: The Good Parts

Highly recommended for beginners and those with a bit more experience alike. Below are some of my brief notes on things that are simple to note down from the book.

  • When invoked with “new”, if a function does not return an object, then “this” is returned.
  • Closure can be used to hide things like nextId generation. var nextId = (function () { var nextId = 0; return function () { return nextId++; }; }());
  • Use closure to have event handlers use value of variables at the time when the handler was bound.
  • Curry = combine a function with values for some parameters, to create a function that requires less parameters. add1 = curry(add, 1).
  • Function memoizer pattern/utility – p.44
  • Object.create – differential inheritance possible from another object
  • Regex group prefixes: ?: noncapturing, ?= positive lookahead, ?! negative lookahead
  • array.sort is not stable: if there are equal elements, they won’t always sort the same. Use a special comparator with a tie breaker function – p.81
  • Regexp.test for true/false, .exec to return null / array of [match, groups…]
  • string.match is like Regexp.exec when no /g flag.
  • string.replace – can take a regex, and the replacer can be a function
  • $& is the matched text, $` is text preceding match, $’ is text following the match
  • is like indexOf, but takes a regex
  • string.split can take a regex
  • string.charCodeAt(index), String.fromCharCode(code)
  • Remember typeof null is “object” – remember when checking
  • parseInt – remember to always pass radix to be safe, usually 10
  • Money calculations – multiply by 100 into pence, calculate, divide back to £
  • Care with special cases of NaN, isNan, isFinite – p.105
  • Is it an array? === ‘[object Array]’. Or Array.isArray in ECMAScript 5
  • Empty string is falsy
  • Care when using objects as maps which may have arbitrary keys – e.g. to do a per-word count. Problems if e.g. the string to count has the word “constructor”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s