javascriptglobal-namespace

Does the javascript global namespace get cleared with each full postback?


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?


Solution

  • 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.