Global Abatement

or, What the heck is global abatement?

tldr: Global variables are code smells. Declaring variables inside an application-level function serves to namespace the variables and minimizes the use of global variables.

Javascript has function-level scoping, which means that variables declared anywhere within a function are scoped to that function. Writing a javascript without an enclosing function leaves your declared variables vulnerable to name collisions, or reassignment by other javascripts.

A global abatement is a strategy used to reduce the vulnerability of your javascript variables by removing them from the global scope. Application namespacing is one such strategy:

BAD GLOBAL
1
2
3
4
person = {
  first_name: 'Hello',
  last_name: 'Kitty'
};
GOOD LOCAL
1
2
3
4
5
var MYAPP = {};
MYAPP.person = {
  first_name: 'Hello',
  last_name: 'Kitty'
};

Declaring an application-level function ensures that the correct person is called when needed. It improves readability by essentially flagging the scope of the variable and reducing ambiguity.

JavaScript: The Good Parts