如何将我的Module-Singleton JavaScript过渡到支持实例? [英] How can I transition my Module-Singleton JavaScript to supporting instances?

查看:73
本文介绍了如何将我的Module-Singleton JavaScript过渡到支持实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在编写应用程序,并且在将不同的功能分解为所谓的模块"模式方面取得了很大的成功,在该模式下,您拥有一个由公共和私有成员自行执行的单例.

I've been writing an application and I have had a lot of success breaking different pieces of functionality into the so called "Module" pattern where you have a self-executing singleton with public and private members.

var WidgetModule = (function($, options) {

    // Private variable
    var someVar;

    // Private functions
    function somePrivateFunction() {

    }

    // Define the public members
    var self = {
        init: function() {

        },
        someFunction: function() {

        }
    };

    return self;

})(jQuery, options);

我现在遇到了一个案例,其中我有几个模块希望创建多个模块.

I have now run into a case where I have several modules which I would like to be able to create multiple instances of.

我知道此模式基于单例,但我想知道是否存在一种轻松的方法来修改此模式以支持创建它们的实例?

I know this pattern is based on the singleton but I wonder if there was a painless way to modify this pattern to support creating instances of them?

推荐答案

当我需要多个对象的通用功能时,这是我通常使用的模式(已调整,以适应您提供的代码):

When I need common functionality for multiple objects, here's the pattern I usually use (adjusted to account for the code you presented):

var Widget = (function($) {
    var pubs = Widget.prototype;

    // Private variable -- global to all instances
    var someVar;

    // The constructor    
    function Widget(options) {
        var privateInstanceVar;

        this.privateInstanceFunc = function() {
            return privateInstanceVar;
        };
    }

    // Private functions -- global to all instances
    function somePrivateFunction() {

    }

    // Define the public members
    pubs.init = function() {

    };

    pubs.someFunction = function() {

    };

    return Widget;

})(jQuery);

用法:

var w = new Widget({someOption: "here"});

如您所见,您可以在构造函数创建的所有实例之间共享私有数据,并且如果您确实愿意,则可以拥有仅与某些选择实例函数共享的私有数据.这些函数必须在构造函数中创建,这具有重用含义,而不需要真正私有实例数据的函数可以在原型上,因此可以被所有实例共享.

As you can see, you can share private data amongst all instances created by the constructor, and if you really want to, you can have private data that's shared only with certain select instance functions. Those functions have to be created in the constructor, which has reuse implications, whereas functions that don't need the truly-private instance data can be on the prototype and therefore be shared by all instances.

更好的是,由于您已经拥有便捷的作用域确定功能,因此可以帮助您工具为您的公共职能提供实际的名称,以帮助您:

Better yet, since you already have a handy scoping function, you can help your tools help you by giving your public functions actual names:

    pubs.init = Widget_init;
    function Widget_init() {

    }

我实际上基本上没有编写上面的代码,因为我定义了一个帮助工厂,使它更加简洁(并且使功能的专业化更加容易,例如CarVehicle继承功能) ); 详细信息.

I mostly don't actually code the above, because I've defined a helper factory that makes it a bit more concise (and makes it easier to do specializations of functionality, like a Car inheriting functionality from Vehicle); details here.

这篇关于如何将我的Module-Singleton JavaScript过渡到支持实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆