Javascript Namespacing

前端 未结 3 1350
遥遥无期
遥遥无期 2020-12-14 23:40

I wish to make my javascript more more modular by splitting it up into different files and giving each file a \'sub\' namespace like so.

subA.js

3条回答
  •  死守一世寂寞
    2020-12-15 00:17

    In you design, you won't have access to private vars/funcs define in that lambda function, because it only return a object, or you want to use new?

    1.Without the (), ex is a function not the object the functions returns.

    2.no need, except you want to pass some parameters for initialization. e.g.

    ex.my = (function(name) {var my_name = name;})('bar');
    

    3.I think you mean this private method.

    ex = (function() {
        var foo = 'bar';
        return {
            show: function() {alert(foo);}
        }
    })();
    

    In the case above, only ex can access foo.

    4.no need, $(document).ready() is called when everything is ready, if you already loaded ex.js, the initialization is done.

    Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded

提交回复
热议问题