var namespaced = {
A: function(){
function r(){
//do some stuff
return something;
}
var someProperty = 5;
function j(){
//do some more stuff
return something;
}
},
B: function(){
//can I call A and C?
A.r();
C.d();
},
C: function(){
function d() {
//do stuff we like
}
}
}
Then I could do…
namespaced.A.j();
namespaced.C.d();
something = namespaced.A.someProperty;
right?
Would I need to do this too?
var something = new namespaced.A()?
If so does A() have a constructor? I’m really confused here :{
I’m trying to encapsulate my javascript so it’s easy to maintain
No you couldn’t. The function
jandsomePropertyare only local toAand are not propagated to the outside. If you want to access them from the outside, you have to make them a property of the function, usingthis:But you would still need to call
var a = new namespaced.A()in order to access the functions.If you want to call
namespaced.A.j()directly, you would have to declareAas object, not as function:So it depends on what you want to achieve eventually… to get a better insight into these methods, I recommend JavaScript Patterns.