我正在尝试创建一个具有createjs EventDispatcher功能的基类,然后从这个基类创建子类,但是如果我尝试对子类实例使用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;
};如何使其工作,以便SubClass从BaseClass继承应用的CreateJS事件分派器功能?到目前为止,只有当我在子类构造函数中应用事件分派程序时才能工作,但这在某种程度上违背了继承的全部目的:
createjs.EventDispatcher.initialize(SubClass.prototype);发布于 2014-01-09 16:36:59
您应该在初始化时,而不是在构造函数中初始化原型。下面是一个更新的小提琴: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;https://stackoverflow.com/questions/21013319
复制相似问题