构建javascript模块的更好方法是什么?

时间:2015-09-28 17:39:52

标签: javascript design-patterns module

我可以在我的js模块上获得一些建议吗?我对js很好,但不是很好的状态:)我是否正在重构我的模块?

我一直在使用这样的js模块模式(粗略的例子,我只是担心结构):

草率的方式?

/* Module Code */
var MapModule = (function ($) {
    var $_address;
    var $_mapContainer;

    function loadApi() {
        // do something. maybe load an API?
    }

    function someInternalMethod() {
        // do other things
    }

    var pub = {};
    pub.setAddress = function (address) {
        $_address = address;
    };
    pub.getAddress = function () {
        return $_address;
    };
    pub.initialize = function () {
        loadApi();
    }
})(jQuery);

// usage
MapModule.initialize();

但这种用法似乎有些草率。我喜欢施工人员。

我重构了一些像这样的模块:

更好的方式?

(function ($) {
    this.MapModule = function () {
        var $_address;
        var $_mapSelector;
        var $_mapContainer;
        function loadApi() {
            // do something. maybe load an API?
        }
        function someInternalMethod() {
            $_mapContainer = $($_mapSelector);
            // do stuff with the jQ object.
        }

        var pub = {};
        pub.setAddress = function (address) {
            $_address = address;
        };
        pub.getAddress = function () {
            return $_address;
        };
        pub.initialize = function (selector) {
            $_mapSelector = selector;
            loadApi();
        }
    }
})(jQuery);

var map = new MapModule();
map.initialize('#mapcontainer');

这种用法对我来说似乎更清洁,而且效果很好,但我能正确地进行吗?

再迈出一步

假设此模块使用包含Google Maps和jQuery功能的div做了一些事情:有关将其转换为jQ插件的任何提示,以便我可以使用var map = $('mapcontainer').mapModule();

这样的签名

谢谢!

2 个答案:

答案 0 :(得分:1)

我修改了你的代码片段并且实际上已经实现了javascript暴露模块模式,这使得有机会实现公共和&私有函数使用闭包。

希望这会有所帮助:

/* Module Code */
var MapModule = (function (module, $, global) {
    var $_address;
    var $_mapContainer;

    // Public functions
    function _loadApi() {
        // Do something, maybe load an API?
    }
    function _someInternalMethod() {
        // Do other things.
    }
    function _initialize = function () {
        _loadApi();
    }

    // Private functions
    function _setAddress = function (address) {
        $_address = address;
    };
    function _getAddress = function () {
        return $_address;
    };

    $.extend(module, {
        loadApi: _loadApi,
        someInternalMethod: _someInternalMethod,
        initialize: _initialize
    });

    return module;
})(MapModule || {},this.jQuery, this);

// Usage
MapModule.initialize();

JSFiddle

答案 1 :(得分:0)

刚刚遇到这个并认为我会分享我的方法......

///////////////////////////////
// Module Code 
///////////////////////////////

var ExampleModule = (function()
{
    ////////////////////////////
    // Private Properties
    ////////////////////////////

    var whatever = {
        data: 'somedata';
    };

    ////////////////////////////
    // Private functions
    ////////////////////////////

    function _init() 
    {
        _loadApi();
        _bindToUIEvents();
    }

    function _loadApi() 
    {
        // load an api
    }

    function _bindToUIEvents()
    {
        $('#something').on('click', function(){

            // Do something cool

        });
    }

    function _getWhatever()
    {
        return whatever;
    }

    //////////////////////
    // Public API
    //////////////////////

    return{

        init: _init(),

        getWhatever: function()
        {
            return _getWhatever();
        }

    };

})();

// Usage
ExampleModule.init;
相关问题