javascript - New properties to Kinetic objects? -
how can add / extend properties kinetic objects through function?
let me explain further. can create new kinetic object this
var car = new kinetic.rect({ width: 15, height: 10});
// , later add custom properties . notation
car.brand = "bmw";
but if want make kinectic objects through function this
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; }
that of course not kinetic object. how can it? possible extend base class able hold custom values?
it considered relatively ugly out of box yes
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");
ask though if car is a kinetic rect. me kind of inheritance doesn't make sense, rather have car property .boundingbox
referring rectangle
instance.
it becomes bit cleaner when extract common code out somewhere:
var oop = { inherits: function(child, parent) { child.prototype = object.create(parent.prototype); child.prototype.constructor = child; return parent.prototype; } };
then code like
var car = (function() { var _super = oop.inherits(car, kinetic.rect); function car(opts, brand) { _super.constructor.apply( this, arguments ); this.brand = brand; } return car; })();
Comments
Post a Comment