Friday, March 30, 2012

JavaScript: The Good Parts by Douglas Crockford

I have really enjoyed reading JavaScript: The Good parts. It has been recommended to me by many friends, and I'm certainly not disappointed. Douglas Crockford did an excellent job.

The book is highly opinionated, and that's a good thing, given that the author is seen as a prolific and talented Javascript expert by his peers. Here is an excerpt that gives you an idea of the book's tone:
The best thing about JavaScript is its implementation of functions. It got almost everything right. But, as you should expect with JavaScript, it didn't get everything right.

Things to remember

  • The prototype link is used only in retrieval. If you set a new property on an object (not on the prototype), it sets it on the receiver only.
  • The hasOwnProperty method does not look at the prototype chain. It looks in the receiver only.
  • Objects are passed around by reference. They are never copied.
  • It is important to understand that the inner function has access to the actual variables of the outer functions and not copies.
  • Objects produced from object literals are linked to Object.prototype
  • Function objects are linked to Function.prototype (which is itself linked to Object.prototype).
  • JavaScript does not have block scope. JavaScript does have function scope.
  • The general pattern of a module is a function that defines private variables and functions; creates privileged functions which, through closure, will have access to the private variables and functions; and that returns the privileged functions or stores them in an accessible place.
  • If all of the state of an object is private, then the object is tamper-proof. Properties of the object can be replaced or deleted, but the integrity of the object is not compromised. If we create an object in the functional style, and if all of the methods of the object make no use of this or that, then the object is durable. A durable object is simply a collection of functions that act as capabilities.

  • It is best to declare all of the variables used in a function at the top of the function body.
  • One way to minimize the use of global variables is to create a single global variable for your application: var MYAPP = {};

Is this book for you?

If you are wondering if you should read that book, you could test yourself. If you fully understand the code snippet below, you can skip it. If not, then you might want to invest in some quality reading. It's an excellent book to beef up your Javascript understanding.

Function.method('bind', function (that) {

  var method = this,
  slice = Array.prototype.slice,
  args = slice.apply(arguments, [1]);

  return function (  ) {
    return method.apply(that,
      args.concat(slice.apply(arguments, [0])));

var x = function (  ) {
  return this.value;
}.bind({value: 666});
alert(x(  )); // 666

1 comment:

Frank Rietta said...

Thanks! I will have to check this book out. Getting into CoffeeScript is also on the todo list sooner rather than later.