如何在我自己的JavaScript类(函数)中实现EvenEmitter?

时间:2015-08-23 01:29:28

标签: javascript events

我喜欢类似节点的编程模型。有谁知道如何在我自己的JavaScript浏览器代码中实现事件?这是一个例子:

function Person () { // Person is an even emitter;
   this.goToWork = function () { 
     this.emit('moved', 100, 100); // work is at 100, 100
   }
}

var person = new Person();
person.goToWork();

person.on('moved', function (x, y) {
  console.log('person moved to ${x} ${y}');
});

如何让我自己的班级Person成为浏览器中的事件发射器?有没有人有用于制作事件发射器的骨架类或示例代码?

0 个答案:

没有答案
相关问题