全局命名空间会被污染是什么意思? [英] What does it mean global namespace would be polluted?

查看:107
本文介绍了全局命名空间会被污染是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全局命名空间会被污染是什么意思?

我真的不明白受污染的全局命名空间是什么意思。

I don't really understand what global namespace getting polluted means.

推荐答案

关于垃圾收集的快速注释

随着变量失去范围,它们将有资格进行垃圾收集。如果它们是全局范围的,那么在全局命名空间失去范围之前它们将不具备收集资格。

As variables lose scope, they will be eligible for garbage collection. If they are scoped globally, then they will not be eligible for collection until the global namespace loses scope.

这是一个例子:

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

将此添加到您的全局命名空间(至少对我而言)应为10,000 kb内存使用情况(win7 firefox)将不会被收集。其他浏览器可能会以不同的方式处理它。

Adding this to your global namespace (at least for me) should ad 10,000 kb of memory usage (win7 firefox) which will not be collected. Other browsers may handle this differently.

而在范围之外的相同代码具有超出此范围的范围:

Whereas having that same code in a scope which goes out of scope like this:

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

允许 arra 在closure执行并有资格进行垃圾回收。

Will allow arra to lose scope after the closure executes and be eligible for garbage collection.

全球命名空间是你的朋友

尽管有许多声称反对使用全球命名空间,这是你的朋友。和一个好朋友一样,你不应该滥用你的关系。

Despite the many claims against using the global namespace, it is your friend. And like a good friend, you should not abuse your relationship.

温柔

不要滥用(通常称为污染)全局命名空间。我的意思是不要滥用全局命名空间 - 不要创建多个全局变量。以下是使用全局命名空间的错误示例。

Don't abuse (usually referred to as "polluting") the global namespace. And what I mean by do not abuse the global namespace is - do not create multiple global variables. Here is a bad example of using the global namespace.

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个全局变量,可能会在某处被覆盖或误解。

This is going to create 11 global variables which could possibly be overwritten or misconstrued somewhere.

资源充足

一种不会污染全局命名空间的资源丰富的方法,将此全部包装在模块模式中,并在暴露多个变量时仅使用一个全局变量。

A more resourceful approach, which does not pollute the global namespace, would be to wrap this all in the module pattern and only use one global variable while exposing multiple variables.

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

Here is an example: (Please note this is simple and there is no error handling)

//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());
})();

这篇关于全局命名空间会被污染是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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