¿Cómo debo pensar en los espacios de nombres de mi aplicación JavaScript?

I'm building an app that has a single global namespace like so:

var myApp = {};

I then have a bunch of different reusable "modules" comprised of models, views and controllers.

//Bar chart module code
org.example.chart.bar.module
org.example.chart.bar.model
org.example.chart.bar.view
org.example.chart.bar.controller

I also have a big dataSource singleton and a dataManager for loading data to the dataSource:

org.example.data.dataSource
org.example.data.dataManager //populates the dataSource with CSV data

And finally translation strings and settings that should be available across the app:

org.example.static.translations
org.example.static.settings

How would you (re-)organize this so that I have easy access to the application level singletons (such as dataSource, dataManager, translations etc.) and can easily instantiate reusable modules that get scoped under the current application instance?

(Would you for example, already from the beginning, use the same namespace for your "classes" and your app? Or would you perhaps make references like so: myApp.translations = org.example.static.translations?)

preguntado el 08 de noviembre de 11 a las 09:11

Phew, I would force you to stop thinking in Java, and start thinking in terms of JavaScript. I get the feeling this is wayyyyy too over-engineered. -

Haha :) OK, so where do I begin? Should I just stuff all my classes under the same namespace? I'd sort of like to have simple endpoints like myApp.translations but at the same time be able to instantiate modules myApp.registeredModules["bar"] = new org.example.chart.bar.module easily ... How to find the right balance? -

You should take a look at the module pattern for structuring javascript in modules and managing dependencies: adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth -

3 Respuestas

No we don't namespace. We write modular code and we use module loaders.

Example of a module loader would be require.js or Navegar or seajs.

And an example module would be something like:

(function () {
  var jQuery = require("jQuery");
  var chart = require("chart"); 

  ...

  define("moduleName", moduleObject);
})();

respondido 08 nov., 11:14

Thanks @Raynos. What would a module for my app level singletons look like? And how would I reference them once loaded? - Dani

@dani you define modules and then load them. Modules are just objects. And loading a module is just loading the object. - Raynos

we don't namespace because we don't add 20 libraries full of cruft just to do a simple animation. - zzzzBov

@Raynos, I'm agreeing with you. With Java or C# you typically add a bunch of import/using statements at the top of a class so that you can use features from each imported library. libA may contain class Foo and libB may also contain class Foo. To keep collisions from occurring you use namespaces. In JavaScript you only add what you need, so there's less of a need for namespaces. - zzzzBov

There is nothing stopping you adding another name to the class. For example.

 org.ds = org.example.data.dataSource;

entonces puedes llamar

 org.ds.getDatasource();

en lugar de

 org.example.data.dataSource.getDatasource();

but both will still work.

EDIT: You could also create other simpler functions that call it taking it out of the oo structure

 var dataSource = function () { return org.example.data.dataSource.getDatasource(); };

respondido 08 nov., 11:13

OK, so you don't see a problem having the shortcut references nor the instantiated objects under the same namespace as the app? Thanks. - Dani

No, the whole reason for using the namespace structure is to avoid conflicts with other libraries of code, but if "org." is your root namespace and nothing else uses it then the only reason not to do it is if you can't remember it is there or you have something else that already uses it. And even so you can write conditional logic to test for this. - kamui

respondido 11 nov., 11:08

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.