I’ve been coming to the opinion for some time now that static methods aren’t the problem. Global variables are the problem. You may think that your code doesn’t have many globals in it, but effectively, it’s littered with immutable global variables:
- Static Methods are immutable globals
- Classes are immutable globals
- Namespaces are immutable globals
If this all sounds a bit like I’m saying “Look at all these trees! There must be a wood nearby!” you’re right. The point I’m making is that techniques such as dependency inversion are all geared to reducing the impact of this immutable baggage we’re carrying around.
- function() just declares a variable. It’s of type function.
- This variable has properties that you can add or remove, just like any other object.
- One of these, prototype is normally considered to be the “class” of the object.
- The prototype, however, is just another instance object.
- Globals is, itself, an instance variable you can manipulate.
But that’s not the coolest bit. The coolest bit is “require”. Require is the function you use in CommonJS to import a module. However,
- Require just returns an instance.
- You can actually replace require (although you’re unlikely to, because it has properties allowing you to tweak its behaviour)
I’m not sure even Clojure allows you to just replace the module loader (although bindings are extremely powerful).
To put it another way, if namespaces aren’t a problem, how come their names are so long?