prototype - Better way to access private members in Javascript -


After reading a bit on javascript, I created a class from

  Variable style Var Some_Class = function () {this.public_method = function () {}; (Function () {// Constructor}). Call (this)}  

to

  var Some_Class = function () {(function ()  

However, I think that this is a good practice, but I am no longer allowed to access private methods in a public way.

  var Some_Class = function () {Var private_member = 'whatever'; (Function () {// constructor}). Call (this)} Some_Class.prototype.public_method = function () {return private_mail; // not possible };  

After reading through an article (Closer-built Constructor), I came out with it

  var Some_Class = function () {var private_member = 'Whatever', private_machine = function (_some_value) {// private method implementation}; If (! Arguments.callee.prototype.public_method) {arguments.callee.prototype.public_method = function () {private_method.call (this, private_method); }; } (Function () {// Constructor}). Call (this)}  

However, what are the drawbacks of doing this ?! Or is there a better way to do this if I want to enter the public system?

Uses function scope Variable and private variables / well-established idioms in the JavaScript community to simulate functions Are there. If the variable is really intended to be private, then I do not see any flaw in this approach (although some claim that some browsers / hosts have to take care of the content code, how many closures are formed).

In your example, private_map (and its environment) is shared in all things - since your public_map closure is created only for the first time when the object is created (and the constructor's prototype is bound to the property Which sets the object's internal prototype series) - hence the only private_map that is used is the one that was created for the first time.

Here is some sample code that indicates what is happening:

 var global = 1; Var Some_Class = function () {var private_method = 'whatever'; Var now = ++ Global; Print ("now external:" now); Private_method = function (_some_value) {// private method implementation prints ("internal now:" now); }; If (! Arguments.callee.prototype.public_method) {arguments.callee.prototype.public_method = function () {private_method.call (this, private_method); }; } (Function () {// constructor}). Call (this)} new Some_Class (). Public_method (); // External Now: 2, Internal Now: 2 New Some_Class (). Public_method (); // External Now: 3, Internal Now: 2 New Some_Class (). Public_method (); // External Now: 4, Internal Now: 2 

Are you sure what you want?

If your personal_moth is not required to reference the position of the attached object, then look for a little benefit in doing something the way you are doing

I What usually I do (if I have to use 'new' to create my object) then the following is done:

 function MyCalus () {var private_var = 1; Function private_func () {} this.public_func = function () {// do something private_func (); } This.public_var = 10; } Var myObj = new MyClass (); 

The downside of this approach is that every time you create an object through the 'new', you create all the closures. But till my profiler tells me that there is no need to customize this design choice, then I like its simplicity and clarity.

In addition, I do not see this in the benefit of your code:

 (function () {}). Call (this); // Call Constructor 

Why are you making a different realm in your constructor?


Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -