Friday, 15 June 2012

JavaScript Visibility: Private and Privileged methods: -



JavaScript Visibility: Private and Privileged methods: -

i trying create sense , understand when utilize private , when utilize privileged methods in javascript. according post: private members in javascript, private members of object can accessed , modified both private methods , privileged methods. when writing getter methods (for private members), utilize private or privileged method? why? accessors (methods modify private members) utilize private or privileged methods? why? standard technique used in cases?

and lastly, difference in using private vs privileged method access private variable (apart fact private method cannot called outside).

function classb(given) { var myvar = given; function _get () { homecoming myvar; }; this.get = function () { myvar = myvar + 3; homecoming myvar; }; }

when designing objects need create decision start. care exposing internals of object outside world?

if don't care, should adding of methods prototype of object, , instance variable this in constructor function. approach may not desirable in situations. privileged methods come in, usage of closures mimic private methods.

when create privileged method, has access private , public members of object you've noted. big difference between creating privileged method , public method on prototype privileged method created every instance of object, whereas public method on prototype created once. if have many privileged methods , creating lot of instances of objects, see how may not desirable when comes memory usage.

i assume looking hide of internal functionality outside world, need create utilize of privileged methods. getters/setters, need define them privileged , add together them this (otherwise there isn't much point in creating getter/setters if plan on leaving them private):

function someclass() { // private fellow member var _privatemember = 1; // public variable this.publicmember = 1; // privileged getter method this.getprivatemember = function() { homecoming _privatemember; }; // privileged setter method this.setprivatemember = function(newval) { _privatemember = newval; }; // private method has access '_privatemember' , 'this' var _privatemethod = function() { // modify private fellow member _privatemember = 2; // modify public fellow member this.publicmember = 1; // other fancy processing privately }; this.dosomethingawesome = function() { // stuff here... // phone call private method maybe? _privatemethod(); }; } // public methods on prototype not have access private methods in constructor someclass.prototype.publicmethod = function() { // cannot access `_privatemember` this.publicmember = 2; // have access 'this' };

as lastly question, using private vs. privileged method modifying private members... you've said, wouldn't able access outside world means pretty useless unless adding other functionality private getter method, , exposing outside world via privileged method. if lot of heavy lifting within constructor of object, valid reason create reusable getter/setter method, depends on utilize case.

if did not reply question was, or if other specific explanation sense free ask.

javascript methods private member

No comments:

Post a Comment