开发者

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() 开发者_JAVA技巧{ return "Rock!"; }
  }
}

Is there a way to do that?


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;


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
  }
};


(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);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜