Implement a pub/sub (publish/subscribe) system in JavaScript.

Instruction: Write a simple implementation of the pub/sub pattern for event handling in JavaScript.

Context: This question tests the candidate's understanding of design patterns and their ability to implement decoupled communication between components.

Updated

Official answer available

Read the opening below, then unlock the full answer and practical guidance.

I would store topics in a Map, so names such as proto are ordinary keys. subscribe registers a callback and returns an unsubscribe function...

Related Questions