11

How to implement Event Emitter in JavaScript? | Facebook Interview Question | De...

 2 years ago
source link: https://devtools.tech/questions/s/how-to-implement-event-emitter-in-javascript-or-facebook-interview-question---qid---J4fNqXImp6QIdGMc7SPF
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to implement Event Emitter in JavaScript? | Facebook Interview Question

JavaScript
Intermediate

Create an event emitter that goes like this

var emitter = new Emitter();

Allows you to subscribe to some event

var sub1 = emitter.subscribe('function_name', callback1);
// you can have multiple callbacks to the same event
var sub2 = emitter.subscribe('function_name', callback2);

You can emit the event you want with this API (you can receive 'n' number of arguments)

emitter.emit('function_name', foo, bar);

And allows you to release the subscription like this (but you should be able to still emit from sub2)

sub1.release();

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK