Would emulating private variables like this be wrong?

R

Ray

Hello,

What do you think about emulating private variables for a class this
way?

function Something() {
var private;
Something.prototype.getPrivate = function() { return private; }
Something.prototype.setPrivate = function(newPrivate) { private =
newPrivate; }
}

var some = new Something("My private");
alert(some.getPrivate());
alert(some.setPrivate("Nyah nyah"));
alert(some.getPrivate());

That way the accessor methods instances are shared among instances of
Something. What do you think?

Thanks!
Ray
 
R

Ray

Ah, silly me. Of course then the private variables are not specific to
instances anymore.

So in other words, there's no way to have a private instance variables
that are accessed through methods attached to the prototype object,
yes? Only instance methods can be getters and setters.

Thanks,
Ray
 
R

Ray

And also private is a reserved word.......

Ah, silly me. Of course then the private variables are not specific to
instances anymore.

So in other words, there's no way to have a private instance variables
that are accessed through methods attached to the prototype object,
yes? Only instance methods can be getters and setters.

Thanks,
Ray
 
R

RobG

Ah, silly me. Of course then the private variables are not specific to
instances anymore.

Please don't top-post here, reply below trimmed quotes.
So in other words, there's no way to have a private instance variables
that are accessed through methods attached to the prototype object,
yes? Only instance methods can be getters and setters.

No - I think you've forgotten about the this keyword. :) Try:

function A(){
this.p;
}

A.prototype.setP = function(x) { this.p = x; }
A.prototype.getP = function() { return this.p; }

var a = new A();
var b = new A();

a.setP('foo');
alert(
'a:p = ' + a.getP() + '\n' +
'b:p = ' + b.getP()
);

b.setP('bar')
alert(
'a:p = ' + a.getP() + '\n' +
'b:p = ' + b.getP()
);
 
R

RobG

No - I think you've forgotten about the this keyword. :) Try:

function A(){
this.p;

}
A.prototype.setP = function(x) { this.p = x; }
A.prototype.getP = function() { return this.p; }

Aggh, sorry, a bit brain-dead this morning... that makes public
members. Of course you need to define private & privileged functions
inside the constructor so that you get closures back to the private
member(s).
 
B

Bruno Desthuilliers

Ray a écrit :
Hello,

What do you think about emulating private variables for a class this
way?

function Something() {
var private;
Something.prototype.getPrivate = function() { return private; }
Something.prototype.setPrivate = function(newPrivate) { private =
newPrivate; }
}

var some = new Something("My private");
alert(some.getPrivate());
alert(some.setPrivate("Nyah nyah"));
alert(some.getPrivate());

That way the accessor methods instances are shared among instances of
Something. What do you think?

That the simple convention of prefixing implementation stuff with a
leading underscore has proven to work very well in some other language.
While data hiding is a mean to enforce encapsulation, encapsulation does
not necessarily imply data hiding !-)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,151
Latest member
JaclynMarl
Top