i am dustin diaz

a JavaScriptr...

boosh.

don't worry about it.

What's new in JavaScript?

Sparked from a late night conversation that I had with my co-author-in-crime, we touched a bit on what prototypal inheritance means for JavaScript, and the role of a constructor function since, if you don't mind me saying, they somewhat contradict each other other. JavaScript has a special operator known as new, which essentially does only _two_ real things for you that couldn't otherwise accomplish without it (although, we might not even possibly need it anyway). The first is simple. You can't achieve instance variables as you do in a classical language. All objects are mutable, and all objects inherit from objects. When you change an object high up in the chain, all objects beneath it, inherit from it through delegation (not concatenation). However "instance variables" is a classical paradigm that in one sense, conflicts with the nature of a prototypal language. Douglas Crockford touches on this briefly in his few words about prototypal inheritance and recommends a utility function to achieve this type of inheritance. The second is rather somewhat obvious as well. We have no real way checking against first class object instances through the instanceof operator. But here we are, conflicted again. If you are in fact concerned about what kind of object you're working with, you can look at your constructor's name property. All in all, by the end of the night, we concluded that there really isn't anything entirely special in nature about the new operator, and that we could write our own special version of it that allows us to embrace true prototypal inheritance:

prototypal "new" in JavaScript

function nue (f) {

    var args = [].slice.call(arguments, 1);

    f.prototype.constructor = f;

    f.apply(f.prototype, args);

    return f.prototype;

}

Embracing prototypal inheritance

The idea is pure, and allows you to truly utilize and incorporate what prototypal inheritance is all about. JavaScript, I believe, was not meant to be a classical language with formal notions of classes, although new gives us a taste of it. So on the other hand, we do not need to "subclass" objects that introduce tightly coupled relationships. Even for classical programmers who come from C, Python or Java, one should know that deep hierarchies are bad, shallow ones are better. JavaScript allows us to avoid hierarchies entirely. So why do it? We can after all, just augment from other objects as necessary, and introduce new objects when needed. JavaScript is unequivocally, a utopian language. It is communistic in nature, meaning that all objects can live in harmony with one another, each sharing the same resources allowing it to be a very, very fast language.

There was in fact "much" more to our conversation, but I'll provide that commentary into our book. Cheers.

this is who i am

Hi, my name is Dustin Diaz and I'm an Engineer @ObviousCorp. Previously @Twitter, @Google, and @Yahoo, author of Strobist® Info co-author of JavaScript Design Patterns, co-creator of the Ender JavaScript Framework, a Photographer, and an amateur Mixologist. This is my website. Welcome!

On this site I write about JavaScript. You can also follow along with my open-source work on Github.

This site is optimized and works best in Microsoft Internet Explorer 6.