实现下面的自定义事件Event对象的接口,功能见注释(测试1)
该Event对象的接口需要能被其他对象拓展复用(测试2)
Event.on('test',function(result){
console.log(result);
})
Event.on('test',function(){
console.log('test');
})
Event.emit('test','hello world');//输出'test'和'hello world'
//测试2
var person1 = {};
var person2 = {};
Object.assign(person1,Event);
Object.assign(person2,Event);
person1.on('call1',function(){
console.log('person1');
});
person2.on('call2',function(){
console.log('person2');
});
person1.emit('call1'); //输出 'person1'
person1.emit('call1'); //未输出
person1.emit('call1'); //未输出
person1.emit('call1'); //输出 'person2'
var Event= {
//通过on接口监听事件evenName
//如果事件eventName被触发,则执行callback回调函数
on:function(eventName,callback){
//代码
},
//触发事件eventName
emit:function(eventName){
//代码
}
}
求指点
Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
认证高级PHP讲师