Is this proper javascript for making a namespace that encapsulates various methods into different objects? -
var namespaced = { a: function(){ function r(){ //do stuff return something; } var someproperty = 5; function j(){ //do more stuff return something; } }, b: function(){ //can call , c? a.r(); c.d(); }, c: function(){ function d() { //do stuff } } }
then do...
namespaced.a.j(); namespaced.c.d(); = namespaced.a.someproperty;
right?
would need too?
var = new namespaced.a()?
if a() have constructor? i'm confused here :{
i'm trying encapsulate javascript it's easy maintain
then do...
namespaced.a.j(); namespaced.c.d(); = namespaced.a.someproperty;
no couldn't. function j
, someproperty
local a
, not propagated outside. if want access them outside, have make them property of function, using this
:
var namespaced = { a: function(){ this.r = function(){ //do stuff return something; }; this.someproperty = 5; this.j = function(){ //do more stuff return something; }; } }
but still need call var = new namespaced.a()
in order access functions.
if want call namespaced.a.j()
directly, have declare a
object, not function:
var namespaced = { a: { r: function(){ //do stuff return something; }, someproperty: 5, j: function(){ //do more stuff return something; } } }
so depends on want achieve eventually... better insight these methods, recommend javascript patterns.
Comments
Post a Comment