0

I keep reading about the dangers of cluttering the global namespace with global variables and functions.

I just read that the 'old' way of defining global functions is problematic especially in situations where you have several developers working on the same project.

I understand and want to write the best code I can but the above statement confuses me.

Rarely do developers work on the same page of a project and I would assume that the global namespace would clear as a user changes pages in the project.

Is this not the case?

Todd Vance
  • 4,627
  • 7
  • 46
  • 66
  • I would suggest the title of this question is not very descriptive of this issue. The answer to the actual question is "yes", the namespace gets cleared. But the issue is about why you need to use some kind of namespacing device. The reason is that you can get identifier clashes ("clutter of the global namespace") – Roland Bouman May 02 '12 at 17:37
  • Well I understand the need. It was the article I was reading that concerned me. I thought the author was suggesting that maybe the global namespace stayed alive within the same domain. (I didn't think this was true but wanted to verify it) – Todd Vance May 02 '12 at 17:45

4 Answers4

1

"Rarely do developers work on the same page of a project"

this assumption is wrong in my experience. In particular, it is good practice to use libraries for often used functionality. The key point of a library is that it is reusable. So, by definition, the library author(s) are "working on your" page.

Roland Bouman
  • 31,125
  • 6
  • 66
  • 67
1

Rarely do developers work on the same page of a project

This is very untrue. Over the course of maintaining a corporate website, several developers can easily be hired and fired and they may all have to work on the same page that you created.

Adam Jenkins
  • 51,445
  • 11
  • 72
  • 100
1

Rarely do developers work on the same page of a project and I would assume that the global namespace would clear as a user changes pages in the project.

Yes, the global namespace is cleared on every request, but that's no reason for polluting the global namespace. You seem to be thinking about a small team of developers, working in the same room. But, even then, what if one developer decides to use some third-party library or widget? If that widget modifies the global namespace, it might interfere with your current code. That's why the best practice is to avoid creating variables/functions on the global namespace.

bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • Thank you! This makes perfect sense. I have only worked on small teams and I want to do what is suggested by those who know better. I was just trying to understand the matter better. – Todd Vance May 02 '12 at 17:40
1

First - yes, when the page reloads all javascript is reinitialized. So if you had the following window.foo would always be one each time the page is loaded:

window.foo = 1;

alert(foo);

window.foo++;

Second - on a team (or in your project) one way you can namespace is like this:

var myCompany = {};

myCompany.Utils = {};

myCompany.Utils.doSomething = function() {
  alert("doSomething");
};

myCompany.Utils.doSomethingElse = function() {
  alert("doSomethingElse");
};

myCompany.Utils.doSomethingElse();

This not only prevents "collisions" within your dev team. It also isolates your code from accidentally "colliding" with another 3rd party javascript referenced on your page.

Kris Krause
  • 7,304
  • 2
  • 23
  • 26