1

I'm running into a couple issues with JavaScript, namespaces and minification/concatenation. I have no control (at least I don't want to have any) over in which order the files get concatenated. Using a namespace function which simply returns an object when given a string such as 'com.example.ExampleClass', a 'class' might not be defined when I need it in another one.

Suppose I have a DocumentProvider object, which needs functionality from EventDispatcher. But, in my current setup, DocumentProvider is concatenated before EventDispatcher. I want to define this functionality on the prototype, so I can't extend it in the constructor.

So, my proposed solution is to use lazy initialization:

  • Define a top level namespace(alias, factory) function
  • Use pseudo-overloading in namespace to either assign a factory to the specified name or return the object created by the factory

Alternatively I could split it up in define(alias, factory) and import(alias). Better for readability, more polluting of global space.

My question is: am I overengineering? Are there better, simpler, more 'standard' solutions to this problem? And what's the risk of namespace functionality (with a different implementation) being added to the language?

1 Answer 1

0

Check out RequireJS It may save you a lot of time.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.