CreateJS EventDispatcher和继承
-
21-12-2019 - |
题
我正在尝试创建一个具有createjs enceDispatcher功能的基类,然后从此基类创建子类,但如果我尝试在子类实例上使用addeventlistener我收到错误:
TypeError: _subClass.addEventListener is not a function
.
我的类的构造函数如下所示:
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;
};
.
如何使其工作,以便子类继承来自BaseClass的应用程序的CreateJS事件调度程序功能?到目前为止,它只有在子类构造函数中应用事件调度程序,但不知何故忽视了继承的整体目的:
createjs.EventDispatcher.initialize(SubClass.prototype);
. 解决方案
您应该在初始化期间初始化原型,而不是在构造函数中。这是一个更新的小提琴: http://jsfiddle.net/lannymccnie/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;
. 不隶属于 StackOverflow