JS: Implementing a Custom Event object interface

Source: Internet
Author: User
Tags emit

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 *         } $};

    • Object.assign(target, ...sources)  可以把任意多个的源对象自身的可枚举属性拷贝给目标对象,然后返回目标对象。

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.