Search code examples
javascriptobjectkineticjsextendprototypal-inheritance

Extend Kinetc.Shape and all derived shapes


I want to extend the object Kinetic.Shape (from which every other shape extends) with some more properties and methods.

What I tried was

Kinetic.Util.addMethods(Kinetic.Shape, {
  foo: 'bar'
});

So say if I created a new Kinetic.Circle instance, it should contain this defined property ( and every other shape should do so to).

new Kinetic.Circle(options).foo; // returns undefined
// should return 'bar'

How can I achieve this behaviour?


Solution

  • Waiting for merge this pull request https://github.com/ericdrowell/KineticJS/pull/497. Now you can do this - replace Kinetic.Util.extend function with this one:

    extend: function(child, parent) {
        function ctor() {
            this.constructor = child;
        }
        ctor.prototype = parent.prototype;
        var old_proto = child.prototype;
        child.prototype = new ctor();
        for (var key in old_proto) {
            if (old_proto.hasOwnProperty(key))
                child.prototype[key] = old_proto[key];
        }
        child.__super__ = parent.prototype;
    }