javascript - Private namespaces for library sub-modules using prototype -


i'm working on large javascript ui module, think in dimension of iscroll. keeping thing sane, split library several logical sub-modules (like: touch interaction, dom interaction, etc.) communicate each other using simple pubsub system.

each of these sub-modules has it's own namespace use storing variables. enable several instances of library running @ same time, constructor includes logic copy every namespace has been assigned 'tocopy' object directly every new instance.

the basic structure looks this:

// constructor  function constructor() {   (var key in this.tocopy) {     // create deep copy , store inside instance     this[key] = utils.copyobj(this.tocopy[key]);   } }  constructor.prototype.tocopy = {}   // sub-module #1  constructor.prototype.tocopy._module1namespace = {}  constructor.prototype._privatefn = function() { /** stuff within this._module1namespace or send events */ } constructor.prototype._privatefn = function() { /** stuff within this._module1namespace or send events */ }   // sub-module #2  constructor.prototype.tocopy._module2namespace = {}  constructor.prototype._privatefn = function() { /** stuff this._module2namespace or send events */ } constructor.prototype._privatefn = function() { /** stuff this._module2namespace or send events */ } 

my problem: dislike having expose namespace of each sub-module rest of library. although i'm disciplined enough not access namespace of sub-module #1 within function of sub-module #2, want technically impossible.

however, several instances of ui library have run @ same time, cannot put every sub-module inside closure , define private namespace inside closure. result in every instance of library accessing same sub-module closure , therefore same namespace.

also, putting inside constructor instead of prototype create new set of functions every new instance, somehow contradicts idea of library.

my research hasn't brought useful patterns problem far, i'm thankful suggestions.

it seems want instances have standard set of properties defined in object, not explicitly assigned in constructor. "standard" pattern be:

function foo(a, b) {   this.a = a;   this.b = b; }  foo.prototype.fn = function() {   // stuff } 

however, seem want create object has standard properties , perhaps default values. maybe should property of constructor:

foo.standardprops = {   a: 'a',   b: 'b' } 

now in constructor can do:

function foo() {   cloneobj(foo.standardprops, this); } 

where cloneobj copy specification.

note there lots of questions , answers how copy or clone javascript object. it's quite difficult in general case there differing opinions on copy vs clone, different types of object , how treat non–object objects arrays, dates , functions , host objects dom elements or window object.

so best define limited case types of object wish make "deep" copies of. may want use getters , setters deal each case explicitly.


Comments

Popular posts from this blog

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

c++ - OpenMP unpredictable overhead -

javascript - Wordpress slider, not displayed 100% width -