Ramon Tayag
Ramon Tayag

Reputation: 16084

JavaScript: Traversing/navigating through a namespace from within a namespace?

I'm new to javascript namespaces, and I found myself kinda deep within a namespace, but unable to find a way to navigate from within the namespace to another object in the same general namespace. It's best described by the code below:

$.fileUploading = {
  images: {
    settings: {
      // How do you do this?
      bing_bong: find.a.way.to.functionOne
    },
    functionOne: function() { return "Rock!"; }
  }
}

Is there a way to do that?

Upvotes: 2

Views: 245

Answers (3)

Praveen Prasad
Praveen Prasad

Reputation: 32117

(function(){

var yourNameSpace={
        publicProp1:publicFn1,
        publicProp2:publicFn2,
        publicProp3:publicFn3
    };

    window.yourNameSpace = yourNameSpace;


//private variable
var _privateVar1,_privateVar2,_privateVar3;

//privatefns
function _privateFn1(){}
function _privateFn2(){}
function _privateFn3(){}
function _privateFn4(){}


//public functions can access private fns 
function publicFn1(){}
function publicFn2(){}
function publicFn3(){}


}(undefined);

Upvotes: 0

Šime Vidas
Šime Vidas

Reputation: 186013

This would work:

$.fileUploading = {
  images: {
    settings: {},
    functionOne: function() { return "Rock!"; }
  }
};

$.fileUploading.images.settings.bing_bong = $.fileUploading.images.functionOne;

This also:

function f() { return "Rock!"; }

$.fileUploading = {
  images: {
    settings: {
      // How do you do this?
      bing_bong: f
    },
    functionOne: f
  }
};

Upvotes: 1

Evert
Evert

Reputation: 99728

Because namespaces are just properties on objects, there's no way to find out what object a property belongs to, from the property. A simple reason is that an identical property can appear in multiple objects.

Namespaces are supposed to be stable and constant, so there's nothing wrong with referencing the entire thing. However, if you need to access the same names a ton of times, you could make it a bit easier for yourself by assigning it to a variable.

var my_ns = $.fileUploading;

Upvotes: 1

Related Questions