发布订阅是一个简单的实用程序类,用于在JavaScript中创建发布/订阅对象。该功能基于发布的PubSub实用程序,最初由David Walsh提出,帮助跨模块通信。\

\

用法:\

通过JavaScript实例化一个PubSub对象,然后进行发布和订阅:\

\


var newPubSub = new PubSub();\

newPubSub.subscribe('aNewTopic', function() { // do things when aNewTopic is published to });\

newPubSub.publish('aNewTopic', { data: 'I want to publish this data to the topic.' });\