CreateJS EventDispatcher and inheritance
-
21-12-2019 - |
Question
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);
Solution
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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow