Javascript 动力学对象的新特性?

Javascript 动力学对象的新特性?,javascript,kineticjs,Javascript,Kineticjs,如何通过函数向动力学对象添加/扩展特性 让我进一步解释。我可以创建一个新的动力学对象,像这样 var car = new Kinetic.Rect({ width: 15, height: 10}); var Car1 = new Car(15, 10, "BMW"); var Car2 = new Car(10, 10, "Volvo"); function Car(width, height, brand) { this.width = width; this.height

如何通过函数向动力学对象添加/扩展特性

让我进一步解释。我可以创建一个新的动力学对象,像这样

var car = new Kinetic.Rect({
width: 15,
height: 10});
var Car1 = new Car(15, 10, "BMW");
var Car2 = new Car(10, 10, "Volvo");

function Car(width, height, brand) {
   this.width = width;
   this.height = height;
   this.brand = brand;
}
//然后使用添加自定义属性。符号

car.brand=“宝马”

但是如果我想通过这样的函数来制作运动物体

var car = new Kinetic.Rect({
width: 15,
height: 10});
var Car1 = new Car(15, 10, "BMW");
var Car2 = new Car(10, 10, "Volvo");

function Car(width, height, brand) {
   this.width = width;
   this.height = height;
   this.brand = brand;
}
那当然不是一个运动物体。但是我该怎么做呢?
是否可以扩展基类以保存自定义值?

可以认为它是相对丑陋的开箱即用,但可以

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
属性的汽车,它指的是一个
矩形
实例


当您从某处提取公共代码时,它会变得更干净一些:

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;
})();

谢谢你,伙计。正如您所说,解决方案似乎有点“丑陋”:)。但这是你需要走的路吗?我的意思是,如果你想用一些额外的数据创建数百个对象,你可能想通过一个函数来实现,对吗?@Walex我的意思是,这是开箱即用的,不需要单独的“继承库”@Walex我添加了一个例子,当你有单独的代码时,可以使用更简洁的方法