将requireJS模块用作单例是不好的做法吗? [英] Is it a bad practice to use the requireJS module as a singleton?

查看:136
本文介绍了将requireJS模块用作单例是不好的做法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我打算使用以下模式来使用基于requireJS的模块作为单例。请注意, classA 会返回类型为classA的实例,而其他类 classB,classC和main 会从模块返回类的类型。所有这些都是基于MooTools类的类。

I plan to use the following pattern to make use of the requireJS based module to act as a singleton. Please notice that classA returns an instance of type 'classA', whereas the rest of the classes classB, classC, and main return the type of the class from the module. All of these are classes based on the MooTools Class.

我们的想法是使用 classA 作为全局可用的单例,方法只是填料。如果这是一种可以使用的模式,是否有任何想法?

The idea is to use the classA as a globally available singleton, the methods are just fillers. Any thoughts if this is an acceptable pattern to use?

这会在稍后阶段回来咬我吗?我还没有尝试在项目上运行r.js,所以我有点担心,并寻找一些建议。

Will this come back to bite me at a later stage? I haven't tried to run r.js on the project yet, so I am a bit worried, and look for some advise.

    // classA.js
    define([], function() {
        var classA = new Class({

            initialize: function (regionId) {
                // perform some Initialization.
                this.data = null;
            },

            doSomething: function(param) {
                // some thing.
                this.data = param;
            }
        };

        return new classA();
    });

    // classB.js
    define(["classA"], function(classA) {
        var classB = new Class({

            initialize: function (regionId) {
                // perform some Initialization.
            },

            doSomethingElse: function() {
                // some thing.
                classA.doSomething("Go back to Work Now!");
            }
        };

        return classB;
    });


    // classC.js
    define(["classA"], function(classA) {
        var classB = new Class({

            initialize: function (regionId) {
                // perform some Initialization.
            },

            doSomethingElse: function() {
                // some thing.
                classA.doSomething("Time to Play!");
            }
        };

        return classC;
    });


    // main.js
    define(["classA", "classB", "classC"], function(classA, classB, classC) {
        var main = new Class({

            initialize: function (regionId) {
                // perform some Initialization.
                this.b = new classB();
                this.c = new classC();
            },

            doEverything: function() {
                // some thing.
                this.b.doSomethingElse();
                classA.doSomething("Nap Time!");
            }
        };

        return main;
    });

非常感谢...

推荐答案

不,我想不出反对使用带有require.js的单例的原因。

No, I cannot think of a reason against using singletons with require.js.

您的模块定义应该导出单例。你这样做很好,因为它是一个单身人士,你也可以避免 new 。我使用类似

Your module definition should export the singleton. The way you do it is fine, since it's a singleton you might be also able to avoid the new. I use something like

define(function (require) {
    var singleton = function () {
        return {
            ...
        };
    };
    return singleton();
});

模块的第一个要求将加载并导出它。需要你的单身人士的其他一些模块只会重复使用已经导出的模块。

The first require to the module will load and export it. Some other module requiring your singleton will just reuse the already exported one.

这篇关于将requireJS模块用作单例是不好的做法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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