这意味着全局名称空间将受到污染是什么意思?
我不太了解被污染的全局名称空间的含义。
Answers:
垃圾收集快速说明
随着变量失去作用域,它们将有资格进行垃圾回收。如果它们是全局作用域的,那么它们将不符合收集条件,直到全局名称空间失去作用域。
这是一个例子:
var arra = [];
for (var i = 0; i < 2003000; i++) {
arra.push(i * i + i);
}
将其添加到全局名称空间(至少对我来说)应该会占用10,000 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());
})();
Calculate.prototype.Slope()在范围外使用返回值之间的区别 吗?完善另一个接近这个问题的概念将是非常完美的!
在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
通常不建议使用全局变量,因为这样可能会导致此类问题。如果我们不使用函数var内部的语句example,那么我们将不小心覆盖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规范。它可能不是最令人兴奋的读物,但它无济于事。