Dan Newcome on technology

I'm bringing cyber back

Javascript zen: the class is the object

with 2 comments

I was reading Douglas Crockford’s book `Javascript: the Good Parts’ when I came across an example involving a parser that was seemingly unrelated to the section that it appeared in (Section 5.3, p51). I re-read the section and then looked at the example again, wondering how such an oversight could have occurred in this book, which had up until this point been impeccably organized.

I decided to look at the example in detail one last time before moving on when I was suddenly enlightened.

In the book, Crockford describes a situation in which he had vigorously defended his position that default fall-throughs on switch statements are useful, only to have someone report a bug in one of his projects the very next day involving just such a switch. In that moment he was enlightened and reversed his position.

In a similar way, I had been a proponent of using the `new’ operator with constructor functions for defining classes and instantiating objects (classical inheritance). However, my background has been in strongly-typed languages, so this is the way that I tend think about the subject. However, the more I use Javascript the more evident it is that you need to avoid trying to shoehorn other development methodologies into it and let it shine at what it does best: prototypal inheritance.

Here is the example from the book:

var block = function (  ) {

// Remember the current scope. Make a new scope that
// includes everything from the current one.

    var oldScope = scope;
    scope = Object.create(scope);

// Advance past the left curly brace.


// Parse using the new scope.


// Advance past the right curly brace and discard the
// new scope, restoring the old one.

    scope = oldScope;

Written by newcome

March 31, 2010 at 10:23 am

Posted in Uncategorized

2 Responses

Subscribe to comments with RSS.

  1. This example stumped me as well, I was wondering what it had to do with the previous prototypical inheritance examples in the chapter (which i understood) …

    What exactly is he doing here… taking a data structure/string? stored in the variable ‘scope’ and parsing it… ?
    Where did ‘scope’ come from? Its not a parameter to the function expression so is it global (highly doubt since he recommends against it)?

    Seems like its not a constructor function as no references to this. etc. (aka i would’t run:
    var x = new block();
    So i assume its a plain old function that would be called via…

    Anyway not really sure why its in this chapter at all if its a regular old function that uses a global variable…
    So i guess I’m asking what it really is 🙂 ?

    Thanks for any input,


    November 4, 2011 at 9:33 am

  2. @ari – yeah there is not a lot of context to clarify things. The point of the example is that it is trivial to ‘fork’ an object instance at will without writing any specific copy constructors or anything like that. The a-ha moment for me was that when you are doing prototypical OO, you don’t have to just think of the base classes as predefined things – any object in the code can become the basis of a new object instance.


    November 4, 2011 at 10:00 am

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

%d bloggers like this: