tags:

views:

63

answers:

4

I am trying to localize everything to a namespace in javascript. So I have objects that follow a naming convention like:

myapp.utilities.file.spinner

etc...

My question is, is there a way to avoid repeating that big string everytime I want to augment the object with a property or a method. Currently my code looks like this...

myapp.utilities.file.spinner.method1 = function() { };
myapp.utilities.file.spinner.method2 = function() { };
etc.

Something like this...

spinnerPath.method1 = function()

...where spinnerPath stands for myapp.utilities.file.spinner, would be nicer. But from my understanding I cannot just say

spinnerPath = myapp.utilities.file.spinner

as that will create another object in the global space.

Thanks

+1  A: 

Try this:

(function(){
  var spinner = myapp.utilities.file.spinner;
  spinner.method1 = function(){};

})();
jvenema
A: 

You can just make a temporary local variable wrapped in an anonymous function:

(function(){
  var spinnerPath = myapp.utilities.file.spinner;
  spinnerPath.method1 = function() { };
  spinnerPath.method2 = function() { };
  spinnerPath.method1();
})();

Here, spinnerPath is in fact a local reference to the global myapp.utilities.file.spinner object, not a copy. Objects in JavaScript are references, so if you create a local variable that points to it, you will not create a copy or pollute the global namespace.

musicfreak
But is this true only when the new variable is local to a function? What if the OP wants to reference `spinnerPath` globally, but as a reference?
Anthony
not reference, sorry. What if the OP wants to use `spinnerPath` globally?
Anthony
That's fine, you could get rid of the anonymous function in that case; but the way I understood the question, he didn't want to pollute the global namespace.
musicfreak
A: 
myapp.utilities.file.spinner.method1 = function() { };
myapp.utilities.file.spinner.method2 = function() { };

...

// Somewhere else in your code, create a temp local called "spinner" 
// that references your longer path object.
var spinner = myapp.utilities.file.spinner;
spinner.method1();
quixoto
+4  A: 

The code you're using won't actually create a new object, merely a new global variable referring to the existing object. It will pollute the global namespace however, so if you're looking to avoid that, you have several options:

  • You can use with, but don't because it will probably cause you more heartache than it's worth.

  • You can make a shorthand pointer variable inside each function outside of the global namespace: var s = myapp.utilities.file.spinner;, but this is annoying.

  • (Probably the best option) create a "private namespace" using an immediate-call function:

    (function (S)  
    {  
       S.method1 = function(){/*whatever*/};  
       S.method2 = function(){/*whatever*/};  
    })(myapp.utilities.file.spinner)
    
Plynx
I like this one the best, but honestly it doesn't make any sense to me. Could you contextualize it a bit more? Just show where the OP actually gets to use the shorthand.
Anthony
@Anthony: he can use it anywhere. All it does is create an anonymous function and immediately call it, passing the object reference as a parameter - naturally, the parameter name is only in scope within the function itself.
Shog9
Shog9 is right--put it anywhere (in the OP's case, probably in the global scope). This is the preferred way, for example, of making jQuery plugins without having to type "jQuery" everywhere (while still respecting jQuery's `noConflict` option).
Plynx
Beautiful. Thank-you.
Travis
A memory sleek solution. Browser gonna love'ya!
Ramiz Uddin