批判我的原型继承模式 [英] Critique my prototypal inheritance pattern

查看:49
本文介绍了批判我的原型继承模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我决定使用 Object.create,因为它似乎比使用new"更直观,例如,必须为每个函数编写 Car.prototype.func1 = function(){};好像有点太干了.

I've decided to use Object.create, as it seems much more intuitive than using 'new' and having to write Car.prototype.func1 = function(){} for each function, for example; seems a bit too DRY.

我顿悟了使用 $.extend 来扩充属性和函数,从而更轻松地从我想使用的任何对象中获取代码.一旦对象被扩展,我就使用 Object.create() 来设置原型,因此这些函数对所有实例都是通用的,并将属性作为第二个参数传入.

I had an epiphany of using $.extend to augment properties and functions, making it easier to grab code from any object I wanted to use. Once the object's extended, I then use Object.create() to set the prototype, so the functions are common to all instances and pass in the properties as the second parameter.

下面的模式好吗?jsfiddle

// props use Object.defineProperties() format; can configure each prop as writable, enumerable, etc

var Vehicle = {
    props : { 'colour' : {value:'black'}, 'wheels' : {value:4} },
    proto : { 'drive' : function(){console.log('drive ' + this.colour + ' ' +     this.wheels);} }
};

var Ferrari = {
    props : { 'colour' : {value:'red'}, 'seats' : {value:2} },
    proto : { 'fast' : function(){console.log('ferrari power ' + this.colour + ' ' + this.wheels + ' ' + this.seats);} }
}; 

function init(){

    // copy Vehicle object, so it remains untouched
    var vehicle = $.extend(true,{}, Vehicle); 

    // augment vehicle super-class with sub-class
    $.extend(vehicle.props, Ferrari.props);
    $.extend(vehicle.proto, Ferrari.proto);

    // prototypal inheritance
    var ferrari = Object.create(vehicle.proto, vehicle.props);
    ferrari.drive();
    ferrari.fast();
}

init();

我已经放弃了这个想法,太乱了;我使用的是原型模式,显示在这篇文章的末尾.>

Edit : I've abandoned this idea, too messy; I'm using a prototypal pattern, shown at the end of this article.

推荐答案

你不应该使用 $.extend 进行继承,你应该在你的类的定义中立即声明继承,而不是稍后在 init 函数中的某个时间.
此外,您的用子类增强车辆超类"似乎真的很倒退.需要复制 Vehicle 对象,因此它保持不变"强调了这一点,并且您正在从 vehicle 创建您的 ferrari 实例,而不是来自 法拉利.

You should not use $.extend for inheritance, and you should declare the inheritance right away at the definition of your class, not somewhen later in an init function.
Also your "augment vehicle super-class with sub-class" seems really backwards. This is emphasized by the need to "copy Vehicle object, so it remains untouched" and that you are creating your ferrari instance from vehicle, not from Ferrari.

我建议使用两个辅助函数:

I'd recommend to use two helper functions:

function inherit(superClass, props, proto) {
    return {
        props: $.extend(Object.create(superClass.props), props),
        proto: $.extend(Object.create(superClass.proto), proto)
    };
}
function create(template) {
    return Object.create(template.proto, template.props);
}

你可以像这样使用

var Vehicle = {
    props : { 'colour' : {value:'black'}, 'wheels' : {value:4} },
    proto : { 'drive' : function(){console.log('drive ' + this.colour + ' ' +     this.wheels);} }
};

var Ferrari = inherit(Vehicle, {
    'colour' : {value:'red'},
    'seats' : {value:2}
}, {
    'fast' : function(){console.log('ferrari power ' + this.colour + ' ' + this.wheels + ' ' + this.seats);}
});

var ferrari = create(Ferrari);
ferrari.drive();
ferrari.fast();

除了这些问题,你的模式还不错.使用纯原型继承是一种既定的模式.您可以通过向模板对象(类)添加初始化函数来修改它,例如命名为.constructor,你又回到了典型类模式的强大之处.

Apart from these problems, your pattern is quite fine. Using pure prototype inheritance is an established pattern. You may amend it by adding an initialisation function to your template objects (classes), named e.g. .constructor, and you're back at the power of the typical class pattern.

这篇关于批判我的原型继承模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆