简体   繁体   中英

Better way to build javascript modules?

Can I get a little advice on my js modules? I'm good with js, but not quite guru status :) Am I refactoring my modules right?

I've been using the js module pattern like this (rough example, I'm just worried about the structure):

sloppy way?

/* 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();

But that usage seems a little sloppy. I like constructors.

I refactored some modules like this:

Better way?

(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');

That usage seems a lot cleaner to me, and it works just fine, but am I going about it properly?

Taking it another step

Say this module does some stuff with a div that wraps Google Maps and jQuery functionality: Any tips on turning that into a jQ plugin so I can use it with a signature like var map = $('mapcontainer').mapModule();

Thanks!

I have modified your snippet and have actually implemented javascript revealing module pattern which gives the opportunity to implement public & private functions using closure.

Hope this will be helpful:

/* 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

Just came across this and thought I'd share my approach...

///////////////////////////////
// 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM