5

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?

Plastic Sturgeon
  • 12,527
  • 4
  • 33
  • 47
  • It could allow you to change the scope of `createjs`. I've never really seen it done like that. – Blender Oct 04 '12 at 00:47
  • How else are you going to expose it if it's not global? – I Hate Lazy Oct 04 '12 at 00:47
  • @user1689607 - its a namespace. My understanding is that you do not want to expose it as a global object. That is why it exists. To reduce the change of a variable naming collision. But I am not sure: hence the question. – Plastic Sturgeon Oct 04 '12 at 00:52
  • Unless you are the only one who is going to use the items in the namespace, you'd need to expose it globally for any other code to make use of it. The only way that `EaselJS` library could be used, would be if it was made accessible on `window`. – I Hate Lazy Oct 04 '12 at 00:55
  • ...or are you saying to expose all the individual members of the namespace globally? – I Hate Lazy Oct 04 '12 at 00:57
  • @user1689607 You indeed have to expose your exported interface through `window` (or another global object), but that's about injecting _all_ library interface into `window`. That's completely different. Imagine, for example, how would jQuery library be used if all methods will be tied up to `window`, and not `window.$` object. – raina77ow Oct 04 '12 at 00:58
  • @raina77ow: Yeah, that would seem bad. It's really not a namespace at that point. That's where I think I was confused. – I Hate Lazy Oct 04 '12 at 01:00
  • There's already enough trouble with different JS libraries using `$` as a shorthand for their namespaces. This idea just seems to be an even worse version of that. – Barmar Oct 04 '12 at 01:03

3 Answers3

5

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.

Lanny
  • 11,244
  • 1
  • 22
  • 30
2

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 window as their namespace root, the harm of methods overwriting methods overwriting another methods will negate any possible advantages of such approach.

raina77ow
  • 103,633
  • 15
  • 192
  • 229
  • I'd love for more detail on this answer, but I'll accept it since it seems to answer the question and no opposing opinions have appeared. Summary: its a bad idea. Thanks. – Plastic Sturgeon Oct 05 '12 at 17:09
1

Actually it depends. If you are using multiple JS libraries, you might be avoiding Namespace to Window to entertain all libraries.

But if you are using a single library (CreateJS) for your game/application, you might consider using Namespace to Window to save a lot of time that will be invested in writing your Custom Namespace name over and over. I find it a handy way when building games with CreateJS.

Fraz Ahmed
  • 288
  • 2
  • 5