为什么我不应该在JavaScript中使用全局变量来保持常量? [英] Why shouldn't I use global variables in JavaScript for something that's constant?

查看:101
本文介绍了为什么我不应该在JavaScript中使用全局变量来保持常量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说JavaScript中的全局变量是不好的,我可以理解一些命名空间问题等等。但是为什么我不应该使用全局变量来处理整个脚本中永远不会改变的东西 - 比如日期? (当然每天都在变化,但是只有被引用,并且在整个脚本中都没有改变。)

解决方案

你可以如果它们在您认为是唯一的命名空间中受保护的情况下使用全局变量,并且只在有益时才使用。主要问题是全局变量可能使一段代码更可能与其他一段代码发生冲突(如果不使用不同的名称空间或非常独特的名称)。出于这个原因和其他原因,最好在实际不需要时避免全局变量(当在某个范围内声明局部变量时也能正常工作),但是仍然有一些适当的原因可以使用全局变量。教育的要点是许多人在完全不需要时使用全局变量。如果你需要它们或者发现它们更高效,那么只要你保护名称空间免受意外碰撞,就可以使用它们。



我亲自创建一个顶级,全局对象并将所有其他全局变量从这一个对象中删除。



全局变量的一些其他问题:


  1. 它们在Javascript中访问速度比本地人慢,因为它们是解释器在可能存在的各个范围内查找给定变量名的最后一个。通常不是一个明显的问题,而是一些要知道。这里有一个 jsperf ,它显示了可能存在的差异。

  2. 如果您有任何修改全局变量的全局或定时器驱动代码的异步代码,并且多个异步操作可能同时处于运行状态,则多个异步操作可以通过修改
  3. 全局通常不会在使用点附近声明,因此阅读代码可能会更具挑战性。 通常不会显示全局自动地在调试器中(局部变量的方式)使调试变得不那么方便。
  4. IE根据DOM中的一些名称自动定义了一些全局变量,这些变量可能会与您自己产生冲突没有你意识到它的全局变量。
  5. 简单地省略局部变量中的关键字var使其成为一个全局变量,并且可以混淆掉代码名称已被用作预期的全局变量。我已经看到这发生在((i = 0; i< m.length; i ++))构造之前。追查什么是错的很难。

  6. 全局变量在脚本的整个生命周期中保持不变。如果使用全局变量来保存对于脚本生命中不需要存在的内容的对象引用,则可能会导致脚本使用的内存比其他内存要多。

  7. 浏览器中的全局变量存在于窗口对象的范围内,因此它们不仅可以与其他全局变量冲突,而且还可以与窗口对象上的任何其他变量冲突。 li>


I've heard that global variables in JavaScript are bad, and I can understand some of the namespace issues, etc. But why shouldn't I use a global variable for something that never gets changed throughout the script--like the date? (Which changes day to day, of course, but only ever gets referenced and not changed throughout the script.)

解决方案

You can use globals if they are protected in what you think is a unique namespace and are only used when beneficial. The main issue is that globals can make one piece of code more likely to conflict with some other piece of code (if not using different namespaces or very unique names). For this reason and others, it is best to avoid globals when they are not actually needed (when variables declared local to some scope would work just as well), but there are still some appropriate reasons to have globals. The main point of education is that many people use globals when they are simply not needed. If you need them or find they are more efficient, then you can use them just fine as long as you protect the namespace from accidental collision.

I personally create one top level, global object and hang all my other globals off that one object.

Some other issues with globals:

  1. They are slower to access in Javascript than locals because they are the last ones found as the interpreter looks for a given variable name in the various scopes that it might exist in. Usually not a noticeable problem, but something to be aware of. Here's a jsperf that shows how much of a difference there can be.
  2. If you have any asynchronous code that modifies globals or timer-driven code that modifies globals and more than one asynchronous operation can be in flight at the same time, the multiple async operations can step on each other through the modification of the same globals.
  3. Globals are often not declared near the point of use so reading code can be more challenging.
  4. Globals are generally not shown automatically in the debugger (the way local variables are) making debugging a little less convenient.
  5. IE automatically defines a bunch of global variables based on some names in the DOM which can conflict with your own global variables without you realizing it.
  6. A simple omission of the keyword "var" on a local variable makes it a global variable and can confuse the heck out of code if that name is already being used as an intended global variable. I've seen this happen on the for (i = 0; i < m.length; i++) construct before. It can be tough to track down what is wrong.
  7. Global variables persist for the life of the script. If one is using a global variable to hold an object reference for something that doesn't need to exist for the life of the script, this can cause the script to use more memory than it otherwise would.
  8. Global variables in a browser exist in the scope of the window object so they can conflict not only with other globals, but also anything else on the window object.

这篇关于为什么我不应该在JavaScript中使用全局变量来保持常量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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