简体   繁体   English

使用standalones扩展脚本,同时保持全球空间清洁

[英]Extending script with standalones whilst keeping global space clean

I'm struggling with how solve this. 我正在努力解决这个问题。

I have some scripts: 我有一些脚本:

  1. A simple plotter, for painting graphs on canvas. 一个简单的绘图仪,用于在画布上绘制图形。
  2. A logger, post and get logs by XMLHttpRequest. XMLHttpRequest记录器,发布和获取日志。
  3. A cookie-handler, general cookie handler. 一个cookie处理程序,一般cookie处理程序。
  4. A IndexedDB script for saving and loading images. 用于保存和加载图像的IndexedDB脚本。

They all work and can be used independently. 它们都可以工作,可以独立使用。 Are self-contained and are kept in separate files. 是自包含的,并保存在单独的文件中。

They have a layout like: 它们的布局如下:

var Plotter = (function () {
    'use strict';
    function Plotter (opt) {

    }

    Plotter.prototype.fun1 () {}
    Plotter.prototype.fun2 () {}

    return Plotter;
})();

Now I have a script where I combine these and in addition do a lot of other stuff. 现在我有一个脚本,我将这些组合在一起,此外还有许多其他的东西。

var my_app = function () {
    this.init = function () {

    }
    this.fun2 = function () {
        this.plotter = new Plotter(opt);
        ...
    }
    this.fun3 = function () { }
};

What I would like is to design this in such a way that I do not have to pollute the global environment with nothing but my_app . 我想要的是以这样的方式设计它,除了my_app我不需要污染全球环境。

I do not want MyPlotter , MyLogger etc as global variables, but as: 我不希望MyPlotterMyLogger等作为全局变量,但是:

my_app = function () {
    var Plot = Plotter;
    ...
    this.fun2 = function () {
        this.plotter = new Plot(opt);
    }
}

Is there a way to solve this? 有办法解决这个问题吗?

In essence extend my_app with the other scripts whilst at the same time keeping the global space unpolluted. 本质上, my_app与其他脚本一起扩展,同时保持全局空间不受污染。

It is perhaps OK when one have two or three extra globals, but not when it becomes much more as in 10-20,.. I do not even like to have two. 当一个人有两个或三个额外的全局变量时,也许没关系,但是当它变得更像10-20时,它就不行了。我甚至不想拥有两个。 Would stay at the one. 会留在那一个。

I obviously have to change things, but a main thing is to continue to keep the files separate such that I can use eg Logger in a separate project. 我显然必须改变一些事情,但主要的是继续保持文件分离,以便我可以在一个单独的项目中使用例如Logger It is easy solved with serverside code to generate a combined space for my_app and the separate scripts, but would rather like to not do it this way. 使用服务器端代码很容易解决为my_app和单独的脚本生成组合空间,但更愿意不这样做。


Edit: 编辑:

After looking at TJ Crowder 's solution I guess I go for a variant of that for now. 在看了TJ Crowder解决方案之后,我想我现在就去找一个变种。 Believe perhaps I'm going to use a library of sorts, as in: 相信也许我会使用各种各样的库,如:

if ('Library' in global) {
    global.Library.Plotter = Plotter;
} else {
    global.Plotter = Plotter;
}

And then in my_app say: 然后在my_app说:

this.Plott = new Library.Plotter();

It still leaves me with two globals, which is one more then I'd like, (I'm perhaps being stupid here), but then it would not be coupled to one name. 它仍然留给我两个全局变量,这是我想要的一个,(我可能在这里很愚蠢),但它不会与一个名称耦合。 An even better solution would perhaps be to always say: 一个更好的解决方案可能是总是说:

if ('Library' in global) {
    global.Library.Plotter = Plotter;
} else {
    global.Library = {Plotter:Plotter};
}

This way it would be consistent across scripts. 这样,它将在脚本之间保持一致。 (Ouch, it looks so stupidly simple now.) (哎呀,现在看起来很简单。)

I know It is not what I asked, but it is perhaps a better way to do it after all. 我知道这不是我所问的,但毕竟这可能是一种更好的方法。 It is a design issue, and I am far from comfortable on if I'm doing it right. 这是一个设计问题,如果我做得对,我很不舒服。

You can do it like this: 你可以这样做:

(function (global) {
    'use strict';
    function Plotter (opt) {

    }

    Plotter.prototype.fun1 () {}
    Plotter.prototype.fun2 () {}

    if ('my_app' in global) {
        global.my_app.Plotter = Plotter;
    }
    else {
        global.Plotter = Plotter;
    }
})(this);

If you want to combine things into my_app , start off with a script that defines my_app in the global namespace as an object: 如果要将事物组合到my_app ,请从一个脚本开始,该脚本将全局命名空间中的my_app定义为对象:

var my_app = {};

// or
this.my_app = {};

If you want to use Plotter on its own, don't, and it will create the Plotter global. 如果你想独立使用Plotter ,那就不要了,它会创建Plotter全局。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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