Stefan
Stefan

Reputation: 14893

JavaScript: Private variables in prototype function

I'm using the prototype function because they are supposed to have a better performance when the "class" is instantiated multiple times. Also not all variables should be accessible to the outside, so they are defined inside the the "class" via var so they are not accessible anywhere outside the closure space.

Now I have this simple example, where I define a "private" variable and define set and get functions for it.

Example:

function Test() {
    var hello = "org";

    this._get = function (value) {
          hello = value;
    }
    this._set = function (value) {            
         return hello;            
    }
}


var test = new Test();
console.log(test._get());
test._set("new");
console.log(test._get());

Fiddler: http://jsfiddle.net/LdwuS/

Now I want to do the same with prototype but the get function always returns undefined!

Example:

function Test() {
    var hello = "org";
}

Test.prototype.set = function (value) {
    return hello;
}
Test.prototype.get = function (value) {
    hello = value;
}

var test = new Test();
console.log(test.get());
test.set("new");

Fiddler: http://jsfiddle.net/rK22m/

Am I doing something wrong or is this not possible? console.log(test.get());

Upvotes: 0

Views: 600

Answers (3)

plalx
plalx

Reputation: 43748

Unfortunately you simply cannot do what you are trying to achieve, because the only way of creating public functions that have access to private variables in JavaScript is to declare the functions in the same scope as the private variables so that the functions creates a closure over these, and then expose the functions publicly.

You have to make a choice on either sacrificing the benefits of using prototypes or sacrificing enforced privacy. A widely adopted solution is to rely on documentation to identity private properties, or to prefix them with a character like _. However, you can always make some functions totally private.

var MyClass = (function () {
    function MyClass() {
        //private
        this._private = 'private';
        this.public = 'public';

        //call privateFunction in the context of the current instance
        privateFunction.call(this);
    }

    //public functions
    MyClass.prototype.publicFunction = function () {
    };

    //private function
    function privateFunction () {
    }

    return MyClass;

})();

Upvotes: 1

ZiggidyCreative
ZiggidyCreative

Reputation: 335

http://jsfiddle.net/uy38G/

doing it this way works

function Test(){
    var hello = "org";   

    this.getHello = function(){
        return hello;
    }

    this.setHello = function(value){
        return hello = value;
    }
}

var test = new Test();

console.log(test.getHello());
test.setHello('new org');
console.log(test.getHello());

Upvotes: -2

Pointy
Pointy

Reputation: 414086

Functions associated with a prototype object have exactly the same sort of access to the object as any other function. Also, like other functions, they have no access to local variables that existed in the constructor function when it was called.

Upvotes: 4

Related Questions