Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Javascript Namespace to Window: Bad idea? Or Brilliant?

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?

like image 678
Plastic Sturgeon Avatar asked Oct 04 '12 00:10

Plastic Sturgeon


1 Answers

The reason this was set up this way was to maintain backwards-compatibility with previous versions which were not namespaced. This allows developers to upgrade to the latest version when it was added without having to refactor all their code.

like image 109
Lanny Avatar answered Oct 16 '22 04:10

Lanny