这意味着全局命名空间将被污染



这意味着全局命名空间会被污染

我真的不明白全局命名空间被污染意味着什么。

垃圾回收快速说明

由于变量失去了作用域,它们将有资格进行垃圾收集。如果它们的作用域是全局的,那么在全局命名空间失去作用域之前,它们将不符合收集条件。

这里有一个例子:

var arra = [];
for (var i = 0; i < 2003000; i++) {
 arra.push(i * i + i);
}

将其添加到全局命名空间(至少对我来说)应该会增加10000 kb的内存使用量(win7 firefox),而这些内存使用量将不会被收集。其他浏览器可能会以不同的方式处理此问题。

而在一个超出范围的范围中有相同的代码,如

(function(){
 var arra = [];
 for (var i = 0; i < 2003000; i++) {
  arra.push(i * i + i);
 }
})();

将允许arra在执行闭包后失去作用域,并有资格进行垃圾收集。

全局命名空间是你的朋友

尽管有很多人反对使用全局命名空间,但它是你的朋友。就像一个好朋友,你不应该滥用你们的关系。

温柔

不要滥用(通常称为"污染")全局命名空间。我所说的不滥用全局名称空间的意思是——不要创建多个全局变量。下面是一个使用全局名称空间的示例。

var x1 = 5;
var x2 = 20;
var y1 = 3
var y2 = 16;
var rise = y2 - y1;
var run = x2 - x1;
var slope = rise / run;
var risesquared = rise * rise;
var runsquared = run * run;
var distancesquared = risesquared + runsquared;
var distance = Math.sqrt(dinstancesquared);

这将创建11个全局变量,这些变量可能在某个地方被覆盖或误解。

足智多谋

一种不污染全局命名空间的更足智多谋的方法是将这一切封装在模块模式中,并且在公开多个变量的同时只使用一个全局变量。

这里有一个例子:(请注意,这很简单,没有错误处理)

//Calculate is the only exposed global variable
var Calculate = function () {
 //all defintions in this closure are local, and will not be exposed to the global namespace
 var Coordinates = [];//array for coordinates
 var Coordinate = function (xcoord, ycoord) {//definition for type Coordinate
   this.x = xcoord;//assign values similar to a constructor
   this.y = ycoord;
  };
  return {//these methods will be exposed through the Calculate object
   AddCoordinate: function (x, y) {
   Coordinates.push(new Coordinate(x, y));//Add a new coordinate
  },
  Slope: function () {//Calculates slope and returns the value
   var c1 = Coordinates[0];
   var c2 = Coordinates[1];
   return c2.y - c1.y / c2.x - c1.x;//calculates rise over run and returns result
  },
  Distance: function () {
   //even with an excessive amount of variables declared, these are all still local
   var c1 = Coordinates[0];
   var c2 = Coordinates[1];
   var rise = c2.y - c1.y;
   var run = c2.x - c1.x;
   var risesquared = rise * rise;
   var runsquared = run * run;
   var distancesquared = risesquared + runsquared;
   var distance = Math.sqrt(distancesquared);
   return distance;
  }
 };
};
//this is a "self executing closure" and is used because these variables will be
//scoped to the function, and will not be available globally nor will they collide
//with any variable names in the global namespace
(function () {
 var calc = Calculate();
 calc.AddCoordinate(5, 20);
 calc.AddCoordinate(3, 16);
 console.log(calc.Slope());
 console.log(calc.Distance());
})();

在JavaScript中,函数之外的声明在全局范围内。考虑这个小例子:

var x = 10;
function example() {
    console.log(x);
}
example(); //Will print 10

在上面的示例中,x是在全局范围中声明的。任何子作用域,例如example函数创建的作用域,都可以有效地继承任何父作用域中声明的内容(在本例中,这只是全局作用域)。

任何重新声明全局作用域中声明的变量的子作用域都会覆盖全局变量,可能会导致不需要的、难以跟踪的错误:

var x = 10;
function example() {
    var x = 20;
    console.log(x); //Prints 20
}
example();
console.log(x); //Prints 10

通常不建议使用全局变量,因为这可能会导致此类问题。如果我们没有在example函数中使用var语句,我们可能会意外地覆盖全局范围中x的值:

var x = 10;
function example() {
    x = 20; //Oops, no var statement
    console.log(x); //Prints 20
}
example();
console.log(x); //Prints 20... oh dear

如果您想阅读更多并正确理解它,我建议您仔细阅读ECMAScript规范。它可能不是最令人兴奋的读物,但它不会有任何帮助。

当您声明全局变量、函数等时,它们(ehm)会进入全局命名空间。除了性能/内存问题(可能会出现)之外,当你重新定义一个重要变量或不使用你认为使用的值时,你很可能会遇到不幸的名称冲突。

要避免在全局名称空间中定义事物。

最新更新