有没有“简明扼要"的?在 JavaScript 中进行命名空间的方法?

Is there a quot;concisequot; way to do namespacing in JavaScript?(有没有“简明扼要的?在 JavaScript 中进行命名空间的方法?)
本文介绍了有没有“简明扼要"的?在 JavaScript 中进行命名空间的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我经常遇到将所有 JavaScript 放在 namespace 结构中的网站:

I've frequently encountered sites that put all of their JavaScript inside a namespace structure along the lines of:

namespaces = { com : { example: { example.com's data} }

但是,相对于其他命名空间框架进行安全设置似乎需要相对大量的代码(定义为 > 2 行).我想知道是否有人知道这样做的简洁方法?此外,是否有一种相对标准/一致的方式来构建它?比如com命名空间是直接附加到全局对象上,还是通过命名空间对象附加?

However, setting this up safely with respect to other namespaced frameworks seems to require a relatively hefty amount of code (defined as > 2 lines). I was wondering whether anyone knows of a concise way to do this? Furthermore, whether there's a relatively standard/consistent way to structure it? For example, is the com namespace directly attached to the global object, or is it attached through a namespace object?

推荐答案

Javascript 没有独立的命名空间.它具有可以提供解析名称范围的功能,以及可以为给定范围内可访问的命名数据提供帮助的对象.

Javascript doesn't have stand-alone namespaces. It has functions, which can provide scope for resolving names, and objects, which can contribute to the named data accessible in a given scope.

这是您的示例,已更正:

Here's your example, corrected:

var namespaces = { com: { example: { /* example.com's data */ } } }

这是一个变量 namespaces 被分配了一个对象字面量.该对象包含一个属性:com,一个具有一个属性的对象:example,该对象可能包含一些有趣的东西.

This is a variable namespaces being assigned an object literal. The object contains one property: com, an object with one property: example, an object which presumably would contain something interesting.

因此,您可以键入 namespaces.com.example.somePropertyOrFunctionOnExample 之类的内容,一切都会奏效.当然,这也很可笑.您没有分层命名空间,您有一个对象,其中包含一个对象,该对象包含您真正关心的东西.

So, you can type something like namespaces.com.example.somePropertyOrFunctionOnExample and it'll all work. Of course, it's also ridiculous. You don't have a hierarchical namespace, you have an object containing an object containing an object with the stuff you actually care about.

var com_example_data = { /* example.com's data */ };

没有无意义的层次结构也同样有效.

That works just as well, without the pointless hierarchy.

现在,如果你真的想要建立一个层次结构,你可以试试这样的:

Now, if you actually want to build a hierarchy, you can try something like this:

com_example = com_example || {};
com_example.flags = com_example.flags || { active: false, restricted: true};

com_example.ops = com_example.ops || (function()
    {
       var launchCodes = "38925491753824"; // hidden / private
       return {
         activate: function() { /* ... */ },
         destroyTheWorld: function() { /* ... */ }
       };
    })();

...恕我直言,相当简洁.

...which is, IMHO, reasonably concise.

这篇关于有没有“简明扼要"的?在 JavaScript 中进行命名空间的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

SCRIPT5: Access is denied in IE9 on xmlhttprequest(SCRIPT5:在 IE9 中对 xmlhttprequest 的访问被拒绝)
XMLHttpRequest module not defined/found(XMLHttpRequest 模块未定义/未找到)
Show a progress bar for downloading files using XHR2/AJAX(显示使用 XHR2/AJAX 下载文件的进度条)
How can I open a JSON file in JavaScript without jQuery?(如何在没有 jQuery 的情况下在 JavaScript 中打开 JSON 文件?)
quot;Origin null is not allowed by Access-Control-Allow-Originquot; in Chrome. Why?(“Access-Control-Allow-Origin 不允许 Origin null在铬.为什么?)
How to get response url in XMLHttpRequest?(如何在 XMLHttpRequest 中获取响应 url?)