Simple inheritance with functions in subclasses It seems like there are many ways implementing inheritance in JavaScript. Which version do you prefer or would you suggest another solution?

The code should run in IE8+ so my first version would not fit…

function Animal(name) {
    this.name = name
}
Animal.prototype = {
    eat: function() {
        console.log(this.name + ': *eat*')
    }
}

function Cat(name) {
    Animal.call(this, name);
}
Cat.prototype = Object.create(Animal.prototype, {
    miaou: {
        value: function() {
            console.log(this.name + ': *miaou*')
        }
    }
})

var garfield = new Cat("Garfield");
garfield.eat();     // Garfield: *eat*
garfield.miaou();   // Garfield: *miaou*


// ------------
//
// possible alternative:

// […] see http://ejohn.org/blog/simple-javascript-inheritance/ for definition
// of Class

var Animal2 = Class.extend({
    init: function(name) {
        this.name = name
    },
    eat: function() {
        console.log(this.name + ': *eat*')
    }
});

var Cat2 = Animal2.extend({
    init: function(name) {
        this._super(name)
    },
    miaou: function() {
        console.log(this.name + ': *miaou*')
    }
});

var garfield2 = new Cat("Garfield2");
garfield2.eat();        // Garfield: *eat*
garfield2.miaou();      // Garfield: *miaou*
As Joseph mentioned, opinion questions is not what CodeReview is about.

I tend to review every OO approach with these 4 questions:

Approach 1


Does instanceof still work ? -> Yes, good
Do you introduce new properties, causing potential naming clashes -> No, good
Can parameters easily be provided to the constructors -> Yes, good
Can I easily have properties in .prototype -> Yes, good


Approach 2


Does instanceof still work ? -> Yes, good
Do you introduce new properties, causing potential naming clashes -> Yes, not so good
Can parameters easily be provided to the constructors -> Yes, good
Can I easily have properties in .prototype -> Yes, good


The second approach uses _super, which is not perfect but definitely acceptable, and the framework was written by John Resig..

In the end, you should use the right tool for the job, and since you provided example code, we can't really tell you which approach is best.