node.js - Prototype / class / public attributes in JavaScript -


i have constructor model (similar backbone model) uses instance of store (e.g. mongodb / redis) passed model factory. inside constructor model, do

this.store = options.store;  

store available this.store once construct instance var model = new model().

then faced situation when model has "public" methods, e.g. model.find() work without instantiating model. because it's not been instantiated, functions inside model can not access store this.store.

what did, started adding store constructor model.store = options.store fixes problem. store exposed uses model constructor , not desirable.

i guessing doing wrong. appreciate help.

if you're saying correctly, think want allow "static" find() method on model without exposing store variable. javascript doesn't have formal private scope classes, can want using closures. here's fiddle:

http://jsfiddle.net/52fpy/

edit: updated fiddle demonstrate various ways expose/hide info using closures: http://jsfiddle.net/52fpy/2/

briefly:

var model = function(){     var store = 'this store';     var model = function(){      }      model.find = function(){         return store;     }      return model; }() 

this "hide" store variable in way want.


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -