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

6
  • 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. Commented Aug 1, 2013 at 15:22
  • 2
    ...and you have your set and get behaviors reversed in the second example. Commented Aug 1, 2013 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. Commented Aug 1, 2013 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 Commented Aug 1, 2013 at 15:32
  • Possible duplicate of stackoverflow.com/questions/436120/… Commented Aug 1, 2013 at 15:53

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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;

})();

Comments

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

2 Comments

You mean doing it just like OP was already doing it in the question's first code example?
The first example hasn't changed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.