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