Approaches to modular client-side Javascript without namespace pollution

后端 未结 8 1295
借酒劲吻你
借酒劲吻你 2021-02-02 02:14

I\'m writing client-side code and would like to write multiple, modular JS files that can interact while preventing global namespace pollution.

index.html



        
8条回答
  •  无人共我
    2021-02-02 02:39

    One way of solving this is to have your components talk to each other using a "message bus". A Message (or event) consists of a category and a payload. Components can subscribe to messages of a certain category and can publish messages. This is quite easy to implement, but there are also some out of the box-solutions out there. While this is a neat solution, it also has a great impact on the architecture of your application.

    Here is an example implementation: http://pastebin.com/2KE25Par

提交回复
热议问题