Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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*
share|improve this question

closed as off-topic by Joseph the Dreamer, syb0rg, Edward, Vogel612, Alex L Apr 21 at 2:20

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions must involve real code that you own or maintain. Questions seeking an explanation of someone else's code are off-topic. Pseudocode, hypothetical code, or stub code should be replaced by a concrete example." – Joseph the Dreamer, syb0rg, Edward, Vogel612, Alex L
If this question can be reworded to fit the rules in the help center, please edit the question.

    
It looks like you're looking for opinions as to which to use, and not a code review. –  Joseph the Dreamer Apr 19 at 23:34

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.