使用JavaScript命名空间有任何危险吗? [英] Are there any dangers associated with using JavaScript namespaces?

查看:114
本文介绍了使用JavaScript命名空间有任何危险吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在创建JavaScript命名空间时是否应该注意任何危险/警告?

Are there any dangers/caveats one should be aware of when creating JavaScript namespaces?

我们的项目相当广泛,我们运行了大量JavaScript文件(20 +,期待更多)。如果不使用命名空间就不可能有任何代码可维护性,所以我们这样实现它们:

Our project is fairly expansive and we are running a lot of JavaScript files (20+, expecting more). It is impossible to have any code maintainability without using namespaces, so we are implementing them like so:

var namespace1 = {

  doSomething: function() {
    ...
  },

  doSomethingElse: function() {
    ...
  }

}

然后创建层次结构,我们将它们链接起来:

And then to create hierarchies, we link them like so:

var globalNamespace = {
  functions1: namespace1,
  functions2: namespace2,
  ...

}

这个工作正常,但它基本上是诀窍让JS表现得好像它确实有名称空间。尽管这种方法得到了很多应用,但大多数关于此的文献似乎都集中在如何做到这一点,而不是是否存在任何可能的缺点。随着我们编写更多JS代码,这很快就会成为我们系统工作方式的一个组成部分。因此,无缝工作非常重要。

This works fine, but it is essentially a "trick" to make JS behave as if it did have namespaces. Although this method gets used a lot, most literature on this seems to focus on how to do it, and not whether there are any possible drawbacks. As we write more JS code, this is quickly becoming an integral part of the way our system works. So it's important that it works seamlessly.

是否存在这种诱导命名空间系统导致错误或需要特别关注的情况?我们可以安全地期望所有浏览器都有相同的行为吗?

Were there any situations in which this "induced" namespace system caused you errors, or otherwise needed special attention? Can we safely expect identical behaviour across all browsers?

推荐答案

在您的示例中定义名称空间的方式似乎是创建全局变量每个命名空间,所以你最终得到

The way you define namespaces in your example it appears to create globals out of each namespace so you end up with

window.namespace1
window.namespace2
window.globalNamespace
window.globalNamespace.namespace1
window.globalNamespace.namespace2

所以如果你有什么那个clobbers window.namespace1 它还会破坏 window.globalNamespace.namespace1

So if you have anything that clobbers window.namespace1 it will also clobber window.globalNamespace.namespace1

编辑:

以下是我们解决此问题的方法:

Here's how we got around this problem:

namespacing = {
    init: function(namespace) {
        var spaces = []; 
        namespace.split('.').each(function(space) {
            var curSpace = window,
                i;  

            spaces.push(space);
            for (i = 0; i < spaces.length; i++) {
                if (typeof curSpace[spaces[i]] === 'undefined') {
                    curSpace[spaces[i]] = {}; 
                }   
                curSpace = curSpace[spaces[i]];
            }   
        });
    }
};

然后你就这样使用它:

namespacing.init('globalNamespace.namespace1');

globalNamespace.namespace1.doSomething = function() { ... };

这样您就不必引入新的全局变量,并且可以放心地添加到现有的命名空间没有破坏其中的其他对象。

This way you don't have to introduce new global variables and you can confidently add to an existing namespace without clobbering other objects in it.

这篇关于使用JavaScript命名空间有任何危险吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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