1
是否可以創建一個對象,它可以像HTMLElements一樣分派事件,並將它用作新類的原型mine?如何創建一個eventdispatcher對象
我知道我可以編寫一個EventDispatcher類來爲我做這件事,但我相信它比使用已經這樣做的本地函數(addEventListener,removeEventListener,dispatchEvent)要慢。
案例1:
var obj = new EventDispatcher();
obj.addEventListener('someevent', listener); // obj can receive listener to a event
obj.dispatchEvent(myEvent); // CustomEvent being dispatched
案例2:
var MyNewClass = function(){};
// MyNewClass now can dispatch events in their instances...
MyNewClass.prototype = Object.create(EventDispatcher.prototype);
var obj2 = new MyNewClass();
obj2.addEventListener('someevent', listener); // works!
你是詢問如何調度DOM事件? – 2013-03-27 16:00:50
CustomEvents沒問題。 – 2013-03-27 16:11:21