forked from able8/hello-nodejs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp5.js
36 lines (28 loc) · 879 Bytes
/
p5.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
var events = require('events')
var util = require('util')
// 事件 对象
var myEmitter = new events.EventEmitter()
// 绑定 事件名称 和 回调函数
myEmitter.on('someEvent', function (message) {
console.log(message)
})
// 触发实践,使用事件名称
myEmitter.emit('someEvent', 'The event was emitted')
var Person = function (name) {
this.name = name
}
// 继承,让Person类具有事件对象的特性,绑定和触发事件
util.inherits(Person, events.EventEmitter)
// 新建对象
var xiaoming = new Person('xiaoming')
var lili = new Person('lili')
var person = [xiaoming, lili]
// 循环person数组,绑定事件
person.forEach(function (person) {
person.on('speak', function (message) {
console.log(person.name + ' said: ' + message)
})
})
// 触发事件
xiaoming.emit('speak', 'hi')
lili.emit('speak', 'I want a curry')