简体   繁体   English

全局变量与将值传递给函数?

[英]Global variables vs. passing a value into a function?

I'm new to JavaScript, and have a simple (I presume) question regarding best practices for accessing variables in functions: 我是JavaScript的新手,并且有一个关于访问函数中变量的最佳实践的简单(我推测)问题:

When should I declare a global variable, as opposed to simple passing a value into a function? 什么时候应该声明一个全局变量,而不是简单地将值传递给函数?

Declaring a global variable should only be used as an option of last resort. 声明全局变量应仅用作最后的选项。

Global variables are bad in general and especially so in javascript. 全局变量一般都很糟糕,特别是在javascript中。 There is simply no way to prevent another piece of javascript from clobbering your global. 根本没有办法阻止另一段javascript破坏你的全局。 The clobbering will happen silently and lead to runtime errors. clobbering将无声地发生并导致运行时错误。

Take the following as an example. 以下面的例子为例。

// Your code
myParam = { prop: 42 };
function operateOnMyParam() {
  console.log(myParam.prop);
}

Here i've declared 2 global variables 在这里,我宣布了2个全局变量

  • myParam myParam
  • operateOnMyParam operateOnMyParam

This might work fine while testing your javascript in isolation. 这可能在单独测试您的javascript时工作正常。 However what happens if after testing a user combines your javascript library with my javascript library that happens to have the following definitions 但是,如果在测试用户之后将您的javascript库与我的javascript库(恰好具有以下定义)相结合,会发生什么

// My code
function myParam() {
  console.log("...");
} 

This also defines a global value named myParam which clashes with your myParam. 这也定义了一个名为myParam的全局值,它与myParam冲突。 Which one wins depends on the order in which the scripts were imported. 哪一个获胜取决于脚本的导入顺序。 But either way one of us is in trouble because one of our global objects is dead. 但无论哪种方式,我们中的一个人都遇到麻烦,因为我们的一个全球物体已经死了

There are many, many reasons.. but an easy one is.. The argument of a function only exists in the function, while it's running. 有许多原因......但是一个简单的原因是..函数的参数只存在于函数中,而它正在运行。 A global variable exists all the time, which means: 全局变量始终存在,这意味着:

  • it takes up memory until you manually 'destroy' it 它会占用内存,直到您手动“销毁”它
  • Every global variable name needs to be unique 每个全局变量名称都必须是唯一的
  • If, within your function.. you call another function.. which ends up calling the first function, all of a sudden you may get unexpected results. 如果,在你的函数中...你调用另一个函数...最终调用第一个函数,突然间你可能会得到意想不到的结果。

In short: because the function argument only lives for a really short time and does not exist outside the function, it's much easier to understand what's going on, and reduced the risk of bugs greatly. 简而言之:因为函数参数只存在很短的时间并且不存在于函数之外,所以更容易理解正在发生的事情,并且大大降低了错误的风险。

When dealing with framework-less JavaScript I'll store my simple variables and functions in an object literal as to not clutter up the global namespace. 在处理无框架JavaScript时,我会将我的简单变量和函数存储在对象文字中,以免混淆全局命名空间。

var myObject = {
   variableA : "Foo",
   variableB : "Bar",
   functionA : function(){
      //do something
      //access local variables
      this.variableA
   }
}

//call functions and variables
myObject.variableA;
myObject.functionA();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM