【问题标题】:JavaScript OOP, inheritance and performanceJavaScript OOP、继承和性能
【发布时间】:2015-04-13 18:20:31
【问题描述】:

我阅读了很多关于此事的文章,也观看了一些视频。但是,我仍然无法理解哪一个以及为什么比另一个更好 - 经典/功能和原型继承。我在发帖前发现和阅读的内容:

我想提一下,我想根据性能、稳定性(容易出错)或其他优点/缺点更好地了解哪种继承类型更好。 我还指出 OOP 和继承不是基于任何库或自定义代码(不包括用于对象创建的 polyfill)。如果使用支持自己的OOP和继承的框架,我会用它,但这里我对那些不感兴趣。

这是我使用原型代码和经典代码编写的一些代码。

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!";
};

我创建了一个 jsperf 测试,可以在这里找到:

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

这表明使用'new'比'Object.create'快得多。

我很高兴听到你的想法。我希望这不是不必要的问题,因为我还没有找到答案。如果我在代码中犯了严重错误,请给我反馈。

【问题讨论】:

  • 您的切线附加问题已回答here。请每个帖子只问一个问题,我已将其删除。
  • 真的没有搜索编辑问题,抱歉。谢谢你的链接!

标签: javascript performance oop inheritance


【解决方案1】:

您可以从强模式提案的字里行间看出什么被认为易于优化https://docs.google.com/document/d/1Qk0qC4s_XNCLemj42FqfsRLp49nDQMZ1y7fwf5YjaI4/view#heading=h.wnixb1advahb 该提案基于具有class 关键字的ES6,但这只是ES5 构造函数+原型分配的语法糖:

ES5:

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

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

ES6(在没有标志的 Chrome 42+ 中工作):

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

    getValue() {
        return this.value;
    }
}

【讨论】:

  • 也许我误解了建议文档中的内容,但构造函数模式和使用'new'似乎更有优势,这与我最近读到的关于邪恶关键字'new'以及为什么必须使用更原生的从对象创建对象。我是否正确编写了示例代码?
  • @Nikola 是的,这与您所读到的相反。如果你注意到了,写这些东西的作者在提出这些声明时可能根本不在乎性能。
  • 实际上,我目前没有找到 object.create 方式的任何优点,因为我没有获得更好的性能,没有封装,而且就个人而言,没有发现它更好地格式化为代码。如果我的假设有误,请提供一些测试或资源来证明否则。谢谢!
  • @Nikola 我当然同意你的观点,只有少数非常口头/权威的人不同意并更喜欢 Object.create 方式。但由于 ES6 包含类,它在未来变得不再是一个问题
猜你喜欢
  • 2016-06-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-23
相关资源
最近更新 更多