简体   繁体   English

Javascript原型类继承

[英]Javascript Prototype Class inheritance

I have done a lot of reading. 我已经读了很多书。 I have written a lot of dummy code. 我写了很多虚拟代码。 I am still as confused as I when I set out to once and for all conquer JavaScript inheritance. 当我一劳永逸地征服JavaScript继承时,我仍然和我一样困惑。 Maybe more so. 也许更多。

I am quite in admiration of Backbone.js and I liked its use of collections. 我非常欣赏Backbone.js并且喜欢使用它的集合。 So I read the source and felt inspired. 因此,我阅读了源代码并感到鼓舞。 But I keep tripping up over inheritance vs extending an object. 但是我总是在继承与扩展对象方面绊倒。

Here is, as best as I can deduce a Javascript class with inheritance, for this use purpose I cannot see the benfit in using extend: 在这里,尽我所能推论带有继承的Javascript类,为此目的,我看不到使用extend的好处:

    function Collection(){
        this._models = [];
    }

    Collection.prototype = {

        models: function() {
            return this._models;
        },

        fetch: function() { 
            console.log("TODO Override fetch method"); 
        },

        add: function(model){
            this._models.push(model);
            $(this).triggerHandler("add", model);
        },
    }

    function Employees(){}

    Employees.prototype = new Collection();

    Employees.prototype.fetch = function(){
        this._models = [new Person("Ron", "male"), new Person("Stevie", "female"),  new Person("David Blunkett", "female")];
    }

It feels a little strange accessing the super classes( Collection ) _models property directly in Employees but it works. 直接在Employees访问super classes( Collection_models属性_models ,但是它可以工作。 Is there anything wrong with this approach? 这种方法有什么问题吗? Any gottchas or caveats? 有任何陷阱或警告吗? I don't want to build an application off of this to find out its a bit... naff. 我不想以此为基础构建一个应用程序,以发现它... naff。

Edit its probably worth noting a Person is a Model too... 编辑它可能值得一提的Person也是Model ...

function Model(){}

Model.prototype = {
    get: function(attr){
        return this.attr;   
    },
    set: function(attr, value){
        return this[attr] = value;
    }
}

function Person(name, gender){
    this.name = name;
    this.gender = gender;
}

Person.prototype = new Model();

Your approach of making a new instance of the parent class and assigning that as the prototype of the child class is what Douglas Crockford calls the "Pseudoclassical" approach to inheritance in JavaScript. 生成父类的新实例并将其分配为子类的原型的方法是Douglas Crockford所说的JavaScript继承中的“伪经典”方法。 Check out this in-depth video where he covers in detail pseudoclassical, prototypal, and parasitic inheritance approaches as well as several variants. 观看这段深入的视频 ,他详细介绍了伪经典,原型和寄生继承方法以及几种变体。 (Pseudoclassical is around 12 minutes in). (伪古典音乐大约在12分钟内)。

Your approach works fine with the one (potentially large) limitation in that you cannot share constructor code in this pattern (at least not without some extra fanciness). 您的方法可以很好地解决一个(可能很大)的局限性,因为您不能以这种模式共享构造函数代码(至少在没有其他幻想的情况下如此)。 So if you do: 因此,如果您这样做:

var e = new Employees();
console.log(e.constructor);

You will see the Collection function instead of the more intuitive Employees function. 您将看到“ Collection功能,而不是更直观的“ Employees功能。 Again, watch that video, it really covers this entire topic very clearly. 同样,观看该视频,它确实非常清楚地涵盖了整个主题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM