它可以被认为是开箱即用的相对丑陋的,但是是的
var Car = (function() {
var _super = Kinetic.Rect.prototype,
method = Car.prototype = Object.create(_super);
method.constructor = Car;
function Car(opts, brand) {
_super.constructor.apply(this, arguments);
this.brand = brand;
}
method.drive = function() {
//lawl
};
return Car;
})();
var bmw = new Car({}, "BMW");
var volvo = new Car({}, "Volvo");
问问自己这辆车是否是动力直角车。对我而言,这种继承没有任何意义,我宁愿拥有一辆具有.boundingBox
引用Rectangle
实例的属性的汽车。
当您将公共代码提取到某个地方时,它会变得更清晰:
var oop = {
inherits: function(Child, Parent) {
Child.prototype = Object.create(Parent.prototype);
Child.prototype.constructor = Child;
return Parent.prototype;
}
};
然后代码看起来像
var Car = (function() {
var _super = oop.inherits(Car, Kinetic.Rect);
function Car(opts, brand) {
_super.constructor.apply( this, arguments );
this.brand = brand;
}
return Car;
})();