-2
は、カスタムイベントオブジェクトのAPIと、ノート(テスト1)で表示される関数を実現します。 イベントオブジェクトのAPIをextend(テスト2)によって再利用する必要がありました。どのようにカスタムイベントオブジェクトのAPIを実現するには?
Event.on('test',function(result){
console.log(result);
})
Event.on('test',function(){
console.log('test');
})
Event.emit('test','hello world');//output 'test'和'hello world'
//test2
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'); //output 'person1'
person1.emit('call1'); //not output
person1.emit('call1'); //not output
person1.emit('call1'); //output 'person2'
var Event= {
//through on api watch event evenName
//if event eventName was triggered,execute callback function
on:function(eventName,callback){
//your code
},
//trigger event eventName
emit:function(eventName){
//your code
}
}
SOコードの書き込みサービスではありません。これは、「ギャップを埋めてください」のように見えます。あなたが試したことと問題がどこにあるのかを教えてくれますか? – Bergi
これはノード上にあると仮定して、組み込みの[EventEmitter](https://nodejs.org/api/events.html)を使用してみませんか? – Bergi