class - Javascript: Revealing Module Pattern. Access private members -
i have javascript library exposing members using 'revealing module pattern'. have particular need need access private member. can me on regard? tips , tricks? highly appreciate if can shed light on this. in advance.
revealing module pattern addy osmani
var myrevealingmodule = (function () { var privatevar = "ben cherry", publicvar = "hey there!"; function privatefunction() { console.log( "name: " + privatevar ); } function publicsetname( strname ) { privatevar = strname; } function publicgetname() { privatefunction(); } // reveal public pointers // private functions , properties return { setname: publicsetname, greeting: publicvar, getname: publicgetname }; })(); myrevealingmodule.setname( "paul kinlan" ); myrevealingmodule.getname(); // there anyway access 'privatefunction'????
is there anyway access 'privatefunction'????
any tips , tricks? highly appreciate if can shed light on this. in advance.
you create object of myrevealingmodule
, contains setname
, greeting
, , getname
. these, setname
references publicsetname
, getname
references publicgetname
. on turn, publicgetname
need: calls privatefunction
.
so, answer question:
myrevealingmodule.publicgetname();
is how access privatefunction
. can change value of privatevar
way:
myrevealingmodule.publicsetname("foobar"); myrevealingmodule.publicgetname();
Comments
Post a Comment