<legend id='352MH'><style id='352MH'><dir id='352MH'><q id='352MH'></q></dir></style></legend>

<small id='352MH'></small><noframes id='352MH'>

<tfoot id='352MH'></tfoot>

      • <bdo id='352MH'></bdo><ul id='352MH'></ul>

        <i id='352MH'><tr id='352MH'><dt id='352MH'><q id='352MH'><span id='352MH'><b id='352MH'><form id='352MH'><ins id='352MH'></ins><ul id='352MH'></ul><sub id='352MH'></sub></form><legend id='352MH'></legend><bdo id='352MH'><pre id='352MH'><center id='352MH'></center></pre></bdo></b><th id='352MH'></th></span></q></dt></tr></i><div id='352MH'><tfoot id='352MH'></tfoot><dl id='352MH'><fieldset id='352MH'></fieldset></dl></div>
      1. 为什么命名空间在 JavaScript 中被认为是不好的做

        时间:2023-09-05
          <tbody id='ck5SA'></tbody>
          <bdo id='ck5SA'></bdo><ul id='ck5SA'></ul>
          <i id='ck5SA'><tr id='ck5SA'><dt id='ck5SA'><q id='ck5SA'><span id='ck5SA'><b id='ck5SA'><form id='ck5SA'><ins id='ck5SA'></ins><ul id='ck5SA'></ul><sub id='ck5SA'></sub></form><legend id='ck5SA'></legend><bdo id='ck5SA'><pre id='ck5SA'><center id='ck5SA'></center></pre></bdo></b><th id='ck5SA'></th></span></q></dt></tr></i><div id='ck5SA'><tfoot id='ck5SA'></tfoot><dl id='ck5SA'><fieldset id='ck5SA'></fieldset></dl></div>
          <tfoot id='ck5SA'></tfoot>
        • <legend id='ck5SA'><style id='ck5SA'><dir id='ck5SA'><q id='ck5SA'></q></dir></style></legend>

        • <small id='ck5SA'></small><noframes id='ck5SA'>

                  本文介绍了为什么命名空间在 JavaScript 中被认为是不好的做法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  有人告诉我不应使用命名空间,因为它们会污染"全局范围.我想知道有什么替代方案?

                  I've been told namespaces shouldn't be used, as they 'pollute' the global scope. I wonder what are the alternatives?

                  当我想定义实用函数和/或常量时,例如对于网站来说,一种简单的方法是通过命名空间来定义它们,这样对全局范围的损害仅限于一个对象.

                  When I want to define utility functions and / or constants e.g. for a website, a simple way to go would be to define them by namespaces, that way the damage to the global scope is restricted to one object only.

                  如果命名空间是不好的做法,我会想到几个问题:

                  If namespaces are bad practice, a couple of questions comes to mind:

                  1. 为什么这是不好的做法?
                  2. 此声明的范围是什么(网络应用程序/动态网站/静态网站等)?
                  3. 有哪些替代方案?

                  这个问题是在 一篇关于使用 extend.js 的好处的帖子.

                  推荐答案

                  为什么这是不好的做法?

                  why is this bad practice?

                  命名空间本身很糟糕,因为它是一个不必要的概念.

                  namespaces themself are bad because it's an unnecessary concept.

                  拥有带有属性和方法的对象是可以接受的.拥有一个类似于命名空间"的模块"令牌也很好,但其含义是每个文件都有一个模块"令牌,其中包含所有属性和方法.这与命名空间"不同,因为它仅在单个文件中创建/更改,并且不作为全局令牌公开.

                  Having objects with properties and methods is acceptable. It's also fine to have a "module" token which is similar to a "namespace" but instead the meaning is that you have one "module" token per file which contains all your properties and methods. This is different from a "namespace" because it's only created / altered in a single file and isn't exposed as a global token.

                  此声明的范围是什么(网络应用程序/动态网站/静态网站等)?

                  what's the scope of this declaration (web applications / dynamic websites / static websites etc.)?

                  所有 ECMAScript,从不创建新的全局令牌

                  All ECMAScript, never create new global tokens

                  有什么选择?

                  而不是拥有命名空间,您应该支持多个文件本地"令牌.这意味着每个文件/模块"都应该包装在一个闭包中,并且您应该可以访问该闭包内的多个局部变量.

                  Rather then having namespaces you should favour multiple "file local" tokens. Meaning that each file / "module" should be wrapped in a closure and you should have access to multiple local variables inside this closure.

                  全局变量也很糟糕,因为您根本不需要它们.避免全局变量的方法是将所有内容包装在闭包中,并在加载外部 javascript 文件时保持智能.

                  Global variables are also bad because you don't need them, at all, ever. The way you avoid globals is by wrapping everything in closures and being intelligent in how you load external javascript files.

                  零全局模块加载器示例

                  • 节点浏览
                  • webmake
                  • modul8

                  进一步阅读:

                  • 没有全局变量的模块化
                  • CommonJS 模块

                  这篇关于为什么命名空间在 JavaScript 中被认为是不好的做法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:有没有“简明扼要"的?在 JavaScript 中进行命名 下一篇:带有函数原型的 Javascript 命名空间声明

                  相关文章

                  最新文章

                  1. <legend id='ROUCK'><style id='ROUCK'><dir id='ROUCK'><q id='ROUCK'></q></dir></style></legend>

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