Questions d'entretien

Entretien pour Front End Engineer

-Menlo Park, CA

Meta

Write an emitter class: /* emitter = new Emitter(); // 1. Support subscribing to events. sub = emitter.subscribe('event_name', callback); sub2 = emitter.subscribe('event_name', callback2); // 2. Support emitting events. // This particular example should lead to the `callback` above being invoked with `foo` and `bar` as parameters. emitter.emit('event_name', foo, bar); // 3. Support unsubscribing existing subscriptions by releasing them. sub.release(); // `sub` is the reference returned by `subscribe` above */

Répondre

Réponses aux questions d'entretien

7 réponse(s)

2

var Emitter = function() { this.events = {}; }; var Subscription = function(event, callback, event_listeners, key) { this.event = event; this.callback = callback; this.event_listeners = event_listeners; this.key = key; }; Subscription.prototype.release = function () { var ret = false; if (this.event_listeners[this.key]) { delete this.event_listeners[this.key]; ret = true; } return ret; }; Emitter.prototype.subscribe = function(event_name, callback) { if (!this.events.hasOwnProperty(event_name)) { this.events[event_name]; this.events[event_name] = []; } var subscription = new Subscription(event_name, callback, this.events[event_name], this.events[event_name].length); this.events[event_name].push(subscription); return subscription; }; Emitter.prototype.emit = function(event_name, param1, param2) { var subs = this.events[event_name]; return subs.forEach(function(sub) { return sub.callback.call(sub, param1, param2); }); }

anonymous le

1

class Emitter { constructor() { this.events = {}; } on(name, handler) { (this.events[name] || (this.events[name] = [])).push(handler); return this.off.bind(this, name, handler); } off(name, handler) { this.events[name] && this.events[name].filter(handle => handle === handler); } emit(name, ...payload) { this.events[name].map(handler => handler(...payload)); } }

osdevisnot le

0

class Emitter { constructor() { this.cb = {}; } subscribe = (name, cb) => { this.cb[name] = cb; return {release: () => delete this.cb[name]}; } emit = (name, ...arges) => { this.cb[name](arges); } }; e = new Emitter(); rm1 = e.subscribe("f1", p => console.log("f1111 ", p)); rm2 = e.subscribe("f2", p => console.log("f222 ", p)); e.emit("f1", "GGG", "ssskoko") e.emit("f1", "GGG", "ssskoko", 1,5,7,2,3)

Utilisateur anonyme le

0

const Emitter = function() { this.events = {} }; Emitter.prototype.subscribe = function( eName, cb ) { this.events[eName] = this.events[eName] || []; this.events[eName].push(cb); return { release: () => { let i = this.events[eName].indexOf(cb); this.events[eName].splice(i, 1); if (!this.events[eName].length) delete this.events[eName]; } } }; Emitter.prototype.emit = function( eName, ...args ) { if ( !this.events[eName] ) throw new Error(`Event ${eName} doesn't exist`); this.events[eName].forEach(el => el.call(this, ...args)); };

Marti le

0

// Off the top of my head. Untested class Emitter { eventToSubscriptions = new Map() subscribe = (eventType, cb) => { let subscriptions = this.eventToSubscriptions.get(eventType) if (!subscriptions) { subscriptions = new Set() this.eventToSubscriptions.set(eventType, subscriptions) } subscriptions.add(cb) return () => { subscriptions.delete(cb) if (!subscriptions.size) { this.eventToSubscriptions.delete(eventType) } } } emit = (eventType, ...args) => { const subscriptions = this.eventToSubscriptions.get(eventType) if (subscriptions) { subscriptions.forEach(cb => cb(...args)) } } }

Utilisateur anonyme le

0

class Emitter { constructor(){ this.subscribedEvents = new Map(); } subscribe(eventName, callback) { this.subscribedEvents.set(eventName, callback); } unsubscribe(eventName) { this.subscribedEvents.delete(eventName); } emit(eventName){ const callback = this.subscribedEvents.get(eventName); if(callback){ const args = Array.from(arguments); args.splice(0, 1); callback.apply(this, args); } } } const emmiter = new Emitter(); emmiter.subscribe("event1", (name, surname)=>{alert[name + " " + surname)}); emmiter.emit("event1", "Hello", "World"); emmiter.unsubscribe("event1"); emmiter.emit("event1", "Hello", "World");

Gorkem Cinar le

0

function Emitter() { this.events = []; this.emit = function(event, ...args) { this.events.forEach(e => { if (e && e.event === event) { e.fn.call(this, args); } }); } this.release = function(index) { this.events[index] = null; } this.subscribe = function(event, fn) { let index = this.events.length; this.events.push({event, fn}); return { release: () => { this.release(index) } } } }

Lennard Schutter le

Ajouter des réponses ou des commentaires

Pour commenter ceci, connectez-vous ou inscrivez-vous.