Identifying Prototype Pattern private variables

I have a problem with private variables in JavaScript revealing a prototype template. I cannot understand how I can use private variables that are used in several different functions inside a common (single) prototype without exposing them. Here is an example of what I mean JSFiddle .

The problem is use var vversus this.v. First, this is the state of all instances, but the second is publicly available. Is there a way to have v private and keep its state for each individual instance?

+4
source share
1 answer

It is not possible to do this with the expand prototype template.

You can only do this with the following:

function MyClass() {
    var v = 1;
    this.getV = function() {
        return v;
    };
}

And that is why there are some smart enthusiasts for this approach.

Personal setting: Attach the front of it underscore and place it on the object: this._v. Do not fight JavaScript; use it.

+6
source

All Articles