<tfoot id='XgOOY'></tfoot>
  1. <small id='XgOOY'></small><noframes id='XgOOY'>

  2. <i id='XgOOY'><tr id='XgOOY'><dt id='XgOOY'><q id='XgOOY'><span id='XgOOY'><b id='XgOOY'><form id='XgOOY'><ins id='XgOOY'></ins><ul id='XgOOY'></ul><sub id='XgOOY'></sub></form><legend id='XgOOY'></legend><bdo id='XgOOY'><pre id='XgOOY'><center id='XgOOY'></center></pre></bdo></b><th id='XgOOY'></th></span></q></dt></tr></i><div id='XgOOY'><tfoot id='XgOOY'></tfoot><dl id='XgOOY'><fieldset id='XgOOY'></fieldset></dl></div>
  3. <legend id='XgOOY'><style id='XgOOY'><dir id='XgOOY'><q id='XgOOY'></q></dir></style></legend>

      <bdo id='XgOOY'></bdo><ul id='XgOOY'></ul>

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

      时间:2023-09-05
        <bdo id='UpAxI'></bdo><ul id='UpAxI'></ul>
        <tfoot id='UpAxI'></tfoot>
          <legend id='UpAxI'><style id='UpAxI'><dir id='UpAxI'><q id='UpAxI'></q></dir></style></legend>

          <small id='UpAxI'></small><noframes id='UpAxI'>

            <tbody id='UpAxI'></tbody>

              • <i id='UpAxI'><tr id='UpAxI'><dt id='UpAxI'><q id='UpAxI'><span id='UpAxI'><b id='UpAxI'><form id='UpAxI'><ins id='UpAxI'></ins><ul id='UpAxI'></ul><sub id='UpAxI'></sub></form><legend id='UpAxI'></legend><bdo id='UpAxI'><pre id='UpAxI'><center id='UpAxI'></center></pre></bdo></b><th id='UpAxI'></th></span></q></dt></tr></i><div id='UpAxI'><tfoot id='UpAxI'></tfoot><dl id='UpAxI'><fieldset id='UpAxI'></fieldset></dl></div>
                本文介绍了有没有“简明扼要"的?在 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 中进行命名空间的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:“隐含全局变量"有哪些问题? 下一篇:为什么命名空间在 JavaScript 中被认为是不好的做

                相关文章

                最新文章

                  <bdo id='WY2hf'></bdo><ul id='WY2hf'></ul>

                <small id='WY2hf'></small><noframes id='WY2hf'>

                <legend id='WY2hf'><style id='WY2hf'><dir id='WY2hf'><q id='WY2hf'></q></dir></style></legend>

                  <tfoot id='WY2hf'></tfoot>

                1. <i id='WY2hf'><tr id='WY2hf'><dt id='WY2hf'><q id='WY2hf'><span id='WY2hf'><b id='WY2hf'><form id='WY2hf'><ins id='WY2hf'></ins><ul id='WY2hf'></ul><sub id='WY2hf'></sub></form><legend id='WY2hf'></legend><bdo id='WY2hf'><pre id='WY2hf'><center id='WY2hf'></center></pre></bdo></b><th id='WY2hf'></th></span></q></dt></tr></i><div id='WY2hf'><tfoot id='WY2hf'></tfoot><dl id='WY2hf'><fieldset id='WY2hf'></fieldset></dl></div>