Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does it mean global namespace would be polluted?

Tags:

javascript

What does it mean global namespace would be polluted?

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

like image 656
theJava Avatar asked Jan 14 '12 14:01

theJava


People also ask

What is global namespace pollution?

Polluting Global namespace causes name collision. This name collision is very common in large projects where we may be using several javascript libraries.

How can we prevent namespace pollution?

Avoiding namespace pollution Javascript don't support function overloading. So when any two functions with same name are used, one function will override the other function depending on the order these functions are loading. This means javascript lacks namespaces(naming convention).

What is the global namespace in javascript?

The global namespace is the top space of Javascript that contains the variables. In a browser, it's known as the window.

What is global namespace scope?

Global scope or global namespace scope is the outermost namespace scope of a program, in which objects, functions, types and templates can be defined. A name has global namespace scope if the identifier's declaration appears outside of all blocks, namespaces, and classes.


1 Answers

Quick Note On Garbage Collection

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.

Here is an example:

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

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

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

Global Namespace Is Your Friend

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

Be Gentle

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); 

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

Be Resourceful

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()); })(); 
like image 96
Travis J Avatar answered Sep 18 '22 19:09

Travis J