Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I've been getting along fine with nothing but util.inherits from NodeJS.

Are there any real advantages to the "set the prototype to this object" approach versus building it up by assigment?

    function Animal(legs) {
        this.legs = legs;
    }
    Animal.prototype.speed = function() {
        return legs * 10;
    }
    
    util.inherits(Dog, Animal);
    function Dog(name) {
        this.constructor.super_.call(this, 4);
        this.name = name;
    }
    
    Dog.prototype.speed = function() {
        // I don't disagree that more sugar here would be good
        return this.constructor.super_.prototype.speed.call(this) * 2;
    }


What your showing is ES3 OO sugar.

The problem I have is that the notion of a constructor function goes against prototypical OO.

In prototypical OO we just have objects and objects inherit from objects. there is no notion of a constructor function.

Also note that pd.make returns an object rather then a function.

It's simply a programming style I like, to think of my "class" object not as the constructor function but as the prototype object.

Not to mention that `x.prototype.foo = ...` is ugly.

    var Animal = {
        constructor: function () {
            this.legs = legs; 
        },
        speed: function () {
            return this.legs * 10;    
        }
    };
    
    var Dog = pd.make(Animal, {
        constructor: function (name) {
            Animal.constructor.call(this, 4);
            this.name = name;
        },
        speed: function () {
            return Animal.speed.call(this) * 2;    
        }
    });




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: