Approaches to modular client-side Javascript without namespace pollution

后端 未结 8 1280
借酒劲吻你
借酒劲吻你 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:58

    I think what you want is https://github.com/component/component.

    It's synchronous CommonJS just like Node.js, it has much less overhead, and it's written by visionmedia who wrote connect and express.

    0 讨论(0)
  • 2021-02-02 02:59

    You can do it like this:

    -- main.js --
    
    var my_ns = {};
    
    -- util.js --
    
    my_ns.util = {
        map: function () {}
        // .. etc
    }
    
    -- index.js --
    
    my_ns.index = {
        // ..
    }
    

    This way you occupy only one variable.

    0 讨论(0)
提交回复
热议问题