NetEase 2017 inside Push pen question
Requirements:
Implement the interface for the following custom events event object, see Comments (Test 1)
The interface of the event object needs to be extended by other objects (test 2)
1 //Test 12Event.on (' Test ',function(Result) {3 Console.log (result);4 });5Event.on (' Test ',function(){6Console.log (' Test ');7 });8Event.emit (' Test ', ' Hello World ');//output ' Hello World ' and ' Test '9 //Test 2Ten varPerson1 = {}; One varPerson2 = {}; A object.assign (Person1, Event); - object.assign (Person2, Event); -Person1.on (' Call1 ',function(){ theConsole.log (' Person1 '); - }); -Person2.on (' Call2 ',function(){ -Console.log (' Person2 '); + }); -Person1.emit (' call1 ');//output ' Person1 ' +Person2.emit (' call2 ');//No output APerson1.emit (' call1 ');//No output atPerson2.emit (' call2 ');//output ' Person2 ' - - - varEvent = { - //Monitor event eventname via on interface - //If event eventname is triggered, the callback callback function is executed inOnfunction(EventName, callback) { - //Your code to }, + //Triggering event EventName -Emitfunction(eventName) { the //Your code * } $};
Here's an article to solve ... But I didn't see http://blog.daraw.cn/2016/08/02/javascript-event-emitter/.
JS: Implementing a Custom Event object interface