JavaScript the Good Parts – Review

First, let’s start with a little joke about JavaScript:

JavaScript good vs evil

See the difference?

 

This book is targeted at helping readers understand the ‘low level’ implementation details of JavaScript.

The code examples on nuances of the language are the best I have found. As a primer, check out Douglas Crockford’s website, which has a lot of the same information in the book.

JavaScript the good parts

The book itself is only about 100 pages. 20% of that is filled with train track diagrams that explain the syntax of JavaScript graphically – not that useful to me personally. There is a good chapter on RegEx (which is more about understanding RegEx than JavaScript). The book is more in depth than his website, but still paper thin relative to a C++ or Java tome. I would have liked to see more examples and the way he might approach setting up a library or a small app.

JavaScript sucks right? It was built for Netscape 2.0.
The only language worse than JavaScript is Action Script 2.0, right??

I think not.

JavaScript is a critical language for the next decade. The forces driving this include:

  • Apple and Google are pushing hard on JavaScript. So is Microsoft with their HTML5 based Windows8.
  • Mobile Browsers – why build a native app when you can get 80-90% of the functionality out of a mobile web app? Marketing firms are asking this question, and opting for mobile web apps vs native apps because in many cases it is more practical. Sure, native apps still rock and are required for many use cases. However, organizations won’t be building multiple native apps targeting Android and iOS, when they can just build one mobile web app that gets the job done.
  • Node.js is to JavaScript what Ruby on Rails was to Ruby back in 2005. We’ll continue to see rapid growth in the Node.js area.

The book points out that to master JavaScript, we must be aware of certain pitfalls:

  • Relies on global variables.
  • ‘+’ is used to add, or to concatenate strings.
  • All variables declared in a function are global to that function – no block scope.
  • Falsy and Truthy are hard to follow when comparing different types. Best to use === and !== which compare on type and value, instead of == and != which compare on value through a type cast if necessary.
  • Functions are redefined at runtime at the top of their scope (eg, a function can be called above its declaration).
  • (list truncated)

The biggest thing I got out of the book, something that every JavaScript programmer needs to know, is the behavior of the ‘new’ keyword:

The behavior the ‘new’ keyword is convoluted and non-obvious. When an object/function is created, adding the new keyword changes the meaning of ‘this’. When ‘new’ is added, ‘this’ refers to the object, which makes sense. However in the next code block, where ‘new’ is omitted, ‘this’ refers to the global Window object! The code example shows how the global object can be polluted. Watch out for ‘this’.

// setup our example function
var Example = function(name) {
	var self = this;
	self.name = name;
	return {
		getName: function () {
			return self.name;
		}
	};
};

// check our Window's name, which should start off blank
console.log("Window's name is: " + window.name);
// -> Window's name is:

// using new keyword
var thisIsSafe = new Example('Safe!');
console.log("thisIsSafe name: " + thisIsSafe.getName());
// -> thisIsSafe name: Safe!

// let's check our Window's name again, should still be blank
console.log("Window's name is: " + window.name);
// -> Window's name is:

// without new keyword
var thisIsBad = Example('Bad!!!');
console.log("thisIsBad name: " + thisIsBad.getName());
// -> thisIsBad name:  Bad!!!
console.log("Window's name is now: " + window.name);
// -> Window's name is now: Bad!!!
// We just polluted the global 'window' variable.
// oh no!!! get away from the fuel tanks!!!

 

Quick JavaScript Tip:

  • CTL+SHIFT+J in Chrome is my friend – brings up the JavaScript console.
  • CTL+SHIFT+J in Firefox brings up the JavaScript error console as well, but it is not as handy as Chrome’s.
This entry was posted in Book Reviews and tagged , , . Bookmark the permalink.

Comments are closed.