小编Kev*_*Ard的帖子

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

我可以在我的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();
Run Code Online (Sandbox Code Playgroud)

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

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

更好的方法?

(function ($) {
    this.MapModule = function () {
        var …
Run Code Online (Sandbox Code Playgroud)

javascript design-patterns module

6
推荐指数
1
解决办法
139
查看次数

标签 统计

design-patterns ×1

javascript ×1

module ×1