0

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());

hexacyanide
  • 88,222
  • 31
  • 159
  • 162
Stefan
  • 14,826
  • 17
  • 80
  • 143
  • 1
    It's not possible to access variables defined inside a function from functions that were defined outside that function. That includes functions on the `.prototype`. –  Aug 01 '13 at 15:22
  • 2
    ...and you have your `set` and `get` behaviors reversed in the second example. –  Aug 01 '13 at 15:24
  • ECMAScript 6 will likely define a "keyed" access to properties, where you can require the key to access certain properties, providing something like private members on objects. –  Aug 01 '13 at 15:31
  • That is already possible (just use some randomly generated string as the key) but it is absurd to take stuff that can be enforced statically to the runtime – Esailija Aug 01 '13 at 15:32
  • Possible duplicate of http://stackoverflow.com/questions/436120/javascript-accessing-private-member-variables-from-prototype-defined-functions – Lukas Aug 01 '13 at 15:53
  • @Lukas : Yes, that's identical to my question – Stefan Aug 02 '13 at 07:06

3 Answers3

4

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.

Pointy
  • 405,095
  • 59
  • 585
  • 614
1

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;

})();
plalx
  • 42,889
  • 6
  • 74
  • 90
-2

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());
ZiggidyCreative
  • 335
  • 3
  • 16