1

I read a lot of articles on the matter, as well as, watched few videos. However, I could still not understand which one and why is better than the other - classical/functional and prototypical inheritance. What I found and have read before posting:

I want to mention that I would like to know better which type of inheritance is better based on performance, stability (error prone), or other prons/cons. I am also pointing to OOP and inheritance not based on any libraries or custom code (excluding polyfill for object create). If a framework that supports own OOP and inheritance is used, I will go with it, but here I am not interested in those.

Here is some code I wrote using both, prototypal one and classical.

var Human = function(name){
        this.name = name;
        return this;
};
Human.prototype.introduce = function(){
        return "I am " + this.name;
};

var Ninja = function(name, level){
        Human.call(this, name);
        this.level = level;
}
Ninja.prototype = Object.create(Human.prototype);//new Human();
Ninja.prototype.introduce = function(){
        var base = Human.prototype.introduce.call(this);
        return base + " My level is " + this.level;
};
Ninja.prototype.fight = function(){
        return (this.name + " can fight");
};

var MasterNinja = function(name, level, masterClass){
        Ninja.call(this, name, level);
        this.masterClass = masterClass;
}
MasterNinja.prototype = Object.create(Ninja.prototype);//new Ninja();
MasterNinja.prototype.introduce = function(){
        var base = Ninja.prototype.introduce.call(this);
        return base + " My master class is " + this.masterClass;
};
MasterNinja.prototype.fight = function(){
        var base = Ninja.prototype.fight.call(this);
        return base + " have master class!";
};              
MasterNinja.prototype.masterFight = function(){
        return this.name + " can master fight!";
};

var human = {
        _init: function(name){
                this.name = name;
                return this;
        },
        introduce: function(){
                return ("Hi, I am " + this.name);
        }
};

var ninja = Object.create(human);
ninja._init = function(name, level){
        human._init.call(this, name);
        this.level = level;
        return this;
};
ninja.introduce = function(){
        var base = human.introduce.call(this);
        return base + " My level is " + this.level;
};
ninja.fight = function(){
        return (this.name + " can fight");
};

var masterNinja = Object.create(ninja);
masterNinja._init = function(name, level, masterClass){
        ninja._init.call(this, name, level);
        this.masterClass = masterClass;
        return this;
};
masterNinja.introduce = function(){
        var base = ninja.introduce.call(this);
        return base + " My master class is " + this.masterClass;
};
masterNinja.fight = function(){
        var base = ninja.fight.call(this);
        return base + " have master class!";
};              
masterNinja.masterFight = function(){
        return this.name + " can master fight!";
};

I created a jsperf test, which could be found here:

http://jsperf.com/js-basic-inheritance

It shows that using 'new' is much faster than 'Object.create'.

I will be glad to hear what you think. I hope this is not considered unnecessary question, since I could not find answer yet. If I have made a critical mistake in my code, please, give me feedback.

3
  • Your tangential, additional question is answered here. please ask one question per post only, I've removed it. Commented Apr 13, 2015 at 19:18
  • I think you'll want to have a look at stackoverflow.com/questions/10898786/… Commented Apr 13, 2015 at 19:34
  • Really did not search for the edit question, sorry for that. Thank you for the links! Commented Apr 13, 2015 at 19:37

1 Answer 1

1

You can read between the lines from the strong mode proposal what is considered easy to optimize https://docs.google.com/document/d/1Qk0qC4s_XNCLemj42FqfsRLp49nDQMZ1y7fwf5YjaI4/view#heading=h.wnixb1advahb The proposal is based on ES6 which has class keyword but this is simply syntactic sugar for ES5 constructor function + prototype assignments:

ES5:

function Class(value) {
    this.value = value;
}

Class.prototype.getValue = function() {
    return this.value;
};

ES6 (Works in Chrome 42+ without flags):

class Class {
    constructor(value) {
        this.value = value;
    }

    getValue() {
        return this.value;
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

maybe I misunderstood the written in the suggested document, but it seems even more pluses for the constructor pattern and using 'new' then, opposite to what I read recently about the evil keyword 'new' and why must use the more native creating objects from objects. Have I written the sample code correctly?
@Nikola Yes, it's opposite of what you have read. If you notice, the authors who write such things probably don't care about performance at all when they make these claims.
Actually, I currently do not find any pluses for the object.create way, since I do not get better performance, no encapsulation and personally, do not find it better formatted as code. Please, provide some tests or resources to prove otherwise if I am wrong in my assumptions. Thank you!
@Nikola I of course agree with you, there is just a very verbal/authorative minority that disagrees and prefers the Object.create way. But since ES6 embraces classes it becomes less an issue in the future

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.