【发布时间】:2020-02-04 15:31:01
【问题描述】:
是否可以在对象中创建自定义事件?像这样的
var myCustomClass = function (param) {
this.param = param;
};
myCustomClass.prototype.start = function(){
//Do staff
//fire event started
let event = new Event("started", {isStarted: true});
this.dispatchEvent(event);
}
并在 mai.js 中创建一个 myCustomClass 的实例
myCustomClass = new myCustomClassr(param);
myCustomClass.addEventListener('started', function () {
console.log("started");
});
myCustomClass.start();
使用此代码,我收到一条错误消息,告诉我 dispatchEvent 不是函数
【问题讨论】:
-
Mr.doob 有一个repository for this
标签: javascript dom-events event-dispatching