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?

8
  • It could allow you to change the scope of createjs. I've never really seen it done like that. Commented Oct 4, 2012 at 0:47
  • How else are you going to expose it if it's not global? Commented Oct 4, 2012 at 0: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. Commented Oct 4, 2012 at 0: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. Commented Oct 4, 2012 at 0:55
  • ...or are you saying to expose all the individual members of the namespace globally? Commented Oct 4, 2012 at 0:57

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.