0

I'm trying to create a base class that has createjs EventDispatcher functionality and then create sub classes from this base class but if I then try to use addEventListener on the subclass instance I get the error:

TypeError: _subClass.addEventListener is not a function

The constructors of my classes look like this:

var BaseClass = function(id)
{
    _instance = this;
    _id = id;
    createjs.EventDispatcher.initialize(BaseClass.prototype);
};

var SubClass = function(id)
{
    BaseClass.call(this, id);
    SubClass.prototype = Object.create(BaseClass.prototype);
    _instance = this;
};

How can I make it work so that SubClass inherits the applied CreateJS event dispatcher functionality from BaseClass? So far it only works if I also apply the event dispatcher in the sub class constructor but that somehow defies the whole purpose of inheritance:

createjs.EventDispatcher.initialize(SubClass.prototype);
BadmintonCat
  • 9,416
  • 14
  • 78
  • 129
  • 1
    If you are using EaselJS 0.7.0 or above, you can inherit from EventDispatcher instead, which is what the EaselJS classes do now. – Lanny Jan 09 '14 at 14:21
  • You mean I should use BaseClass.prototype = new createjs.EventDispatcher(); in my base class? But even that doesn't fix my problem. – BadmintonCat Jan 10 '14 at 05:13

1 Answers1

0

You should initialize your prototypes during initialization, not in the constructor. Here is an updated fiddle: http://jsfiddle.net/lannymcnie/qTHb4/

var BaseClass = function(id)
{
    _instance = this;
    _id = id;
};
createjs.EventDispatcher.initialize(BaseClass.prototype);

var SubClass = function(id)
{
    BaseClass.call(this, id);
    _instance = this;
};
SubClass.prototype = BaseClass.prototype;
Lanny
  • 11,244
  • 1
  • 22
  • 30
  • Did that but doesn't work. Still getting the error. Btw, initialization happens when in JS? I suppose it is code executed right after a JS file has been loaded? – BadmintonCat Jan 10 '14 at 05:14
  • If you put it in the constructor, you have to wait until the constructor is called (using `new class()`). The fiddle I posted works with the changes. Let me know if you are still having trouble. – Lanny Jan 14 '14 at 15:25