Javascript命名空间-这是一个好模式吗? [英] Javascript Namespace - Is this a good pattern?

查看:79
本文介绍了Javascript命名空间-这是一个好模式吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目标...

  1. 从全局对象中删除变量,对象等.
  2. 消除碰撞的可能性.

首先,我实现Yahoo命名空间代码(出于示例目的,我将ROOT用作命名空间的根目录)...

Firstly I implement the Yahoo namespace code (note for example purposes I am using ROOT as the root of my namespace)...

        if (typeof ROOT == "undefined" || !ROOT) {
                var ROOT = {};
        }

        ROOT.namespace = function () {
            var a = arguments,
                o = null,
                i, j, d;
            for (i = 0; i < a.length; i = i + 1) {
                d = ("" + a[i]).split(".");
                o = ROOT;
                for (j = (d[0] == "ROOT") ? 1 : 0; j < d.length; j = j + 1) {
                    o[d[j]] = o[d[j]] || {};
                    o = o[d[j]];
                }
            }
            return o;
        }

现在我声明我的第一个命名空间...

Now I declare my 1st namespace...

ROOT.namespace("UI");

            ROOT.UI = {
                utc: 12345,
                getUtc: function() {
                    return this.utc;
                }
            }

我在这里想要做的是保存我的UI所需的var(在这种情况下,该时间是UTC的当前时间),以便它们不在全局对象上.我还想提供一些特定的功能.应该在每个页面上都可以使用,而无需任何形式的说明.

What I want to do here is to hold vars that I need for my UI (in this case the current time in UTC) so that they are not on the global object. I also want to provide some specific functionality. This should be available on every page without any sort of instanciation...

现在,我想在我的命名空间结构中存储一个对象.但是,将需要多次创建此对象.此处的目的是将其保留在我的结构中,但允许根据需要创建它多次.如下:

Now I want to have an object stored within my namespace structure. However, this object will need to be created multiple times. The objective here is to keep this inside my structure but allow it to be created as many times as I need. This is as follows:

 ROOT.namespace("AirportFinder");
            ROOT.AirportFinder = function(){ 
                this.var1 = 99999;

                this.Display = function() {
                    alert(this.var1);
                }            
            }

这是实例化对象的示例代码...

And this is the sample code to instanciate the object...

        var test1 = new ROOT.AirportFinder();
        test1.Display();

这是一个好模式吗?

推荐答案

在名称空间ROOT或类似名称上进行定义确实是合理的.

It is indeed reasonable to have things defined on a namespace ROOT or something alike.

最好使用闭包

(function() {
    var AirportFinder = function() { 
        this.var1 = 99999;
        this.Display = function() {
            alert(this.var1);
        }            
    };

    // do Stuff with local AirportFinder here.

    // If neccesary hoist to global namespace
    ROOT.AirportFinder = AirportFinder;
}());

如果它们不需要是全球性的.我本人使用别名($.foobar,因为jQuery无论如何都是全局的)来存储任何全局数据.

If they don't need to be global. I myself use an alias ($.foobar because jQuery is global anyway) for storing any global data.

恐怕我不能告诉你.namespace函数能做到吗.没必要.

I'm afraid I can't tell you waht the .namespace function does. It's not really neccessary.

我个人的喜好是始终使用闭包来创建私有名称空间,并将所有内容提升到需要的全局/共享名称空间.这样会将全局可见性/集群降至最低.

My personal preference is to always use closures to create a private namespace and hoist anything to the global/shared namespace where neccesary. This reduces the global visibility/cluster to a minimum.

这篇关于Javascript命名空间-这是一个好模式吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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