I cam upon this code in an example for the EaselJS library – what it does is it assigns the namespace of the entire createjs library to “window”.
<script>
var createjs = window;
</script>
My question is this:
Is setting the namespace of a library to window a really dumb idea? Doesn’t it just get rid of the whole point of using a namespace by making all the namespaced variable suddenly global scoped?
The only advantage I can see is letting you write shorter contructors for your objects. For example typing:
stage = new Stage(canvas);
instead of:
stage = new createjs.Stage(canvas);
Is this a bad idea, or is is somehow brilliant, or just harmlessly quirky?
A good idea for me is something that should be actively used by many people. And that’s exactly why I consider this trick a bad idea: in short, it actually defeats the idea of namespaces: if many people (= authors of other popular JS libraries) start to use
windowas their namespace root, the harm of methods overwriting methods overwriting another methods will negate any possible advantages of such approach.