JavaScript自动执行匿名模块

JavaScript自动执行匿名模块,javascript,Javascript,在此基础上,我想知道如何创建属于同一名称空间(项目)的新模块 模块 (function( A, $, undefined ) { A.init = function() { console.log( "A init" ); }; }( window.A = window.A || {}, jQuery )); (function( B, $, undefined ) { B.init = function() { console.l

在此基础上,我想知道如何创建属于同一名称空间(项目)的新模块

模块

(function( A, $, undefined ) {
    A.init = function() {
        console.log( "A init" );
    };   
}( window.A = window.A || {}, jQuery ));

(function( B, $, undefined ) {
    B.init = function() {
        console.log( "B init" );
    };   
}( window.B = window.B || {}, jQuery ));

A.init();
B.init();

只需将一个对象添加到全局名称空间,将对象文本或函数分配到该名称空间

window.PROJECT = {};
(function($,window,undefined) {
    var A = {
        init : function() { ... }
    }
    window.PROJECT.A = A;
})(jQuery, window);
PROJECT.A.init();
或者,您可以将模块中的值返回到项目对象

window.PROJECT = {};
PROJECT.A = (function($, window, undefined) {
    var A = {
        init : function() { ... }
    }
    return A;
})(jQuery,window);
同样,您可以将对象返回到全局变量

var PROJECT = (function($, window, undefined) {
    var A = {
        init : function() { ... }
    }
    var B = {
        init : function() { ... }
    }
    return { A : A, B : B };
})(jQuery,window);
基于OP前面提到的答案,扩展了一个全局名称空间对象。这实际上已经通过前面的答案实现了

var PROJECT = (function(window, undefined) {
    // Private var
    var container,
    // Class constructor
    Example = function() {
    }
    Example.prototype = {
    },
    // Object literal
    A = { 
        init : function() {
            container = new Example();
        }
        // Expose or reflect other methods using private instance of Example
    }
    return { A : A };
})(window);
要进一步扩展项目,请按照前面的示例执行

(function(window, PROJECT, undefined) {
     // Private vars
     ...
     // Any other non exposed code
     ...
     PROJECT.B = {
         init : function() { ... }
     }
     // Make sure PROJECT is attached to window if it is empty object
     if (typeof window.PROJECT === 'undefined')
         window.PROJECT = PROJECT;
})(window, window.PROJECT || {});

只需将附加命名空间插入属性链:

// create top namespace
window.PROJECT = window.PROJECT || {};

// stays the same
(function( A, $, undefined ) {
    A.init = function() {
        console.log( "A init" );
    };
// except for the last line:
}( window.PROJECT.A = window.PROJECT.A || {}, jQuery ));

// same for the B (sub)namespace:
(function( B, $, undefined ) {
    …  
}( window.PROJECT.B = window.PROJECT.B || {}, jQuery ));

// and of course add it at the invocations:
PROJECT.A.init();
PROJECT.B.init();

你试过什么吗?太好了!除非调用应该是PROJECT.A.init();和PROJECT.B.init();
// create top namespace
window.PROJECT = window.PROJECT || {};

// stays the same
(function( A, $, undefined ) {
    A.init = function() {
        console.log( "A init" );
    };
// except for the last line:
}( window.PROJECT.A = window.PROJECT.A || {}, jQuery ));

// same for the B (sub)namespace:
(function( B, $, undefined ) {
    …  
}( window.PROJECT.B = window.PROJECT.B || {}, jQuery ));

// and of course add it at the invocations:
PROJECT.A.init();
PROJECT.B.init();