473,394 Members | 1,701 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Would emulating private variables like this be wrong?

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

Jan 26 '07 #1
7 1345
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

On Jan 27, 12:22 am, "Ray" <ray_use...@yahoo.comwrote:
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
Jan 26 '07 #2
Ray
And also private is a reserved word.......

On Jan 27, 12:39 am, "Ray" <ray_use...@yahoo.comwrote:
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

On Jan 27, 12:22 am, "Ray" <ray_use...@yahoo.comwrote:
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
Jan 26 '07 #3


On Jan 27, 2:22 am, "Ray" <ray_use...@yahoo.comwrote:
Hello,

What do you think about emulating private variables for a class this
way?
Try Richard Cornford's article on private static members:

<URL: http://www.litotes.demon.co.uk/js_in...te_static.html >

--
Rob

Jan 27 '07 #4


On Jan 27, 2:39 am, "Ray" <ray_use...@yahoo.comwrote:
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()
);
--
Rob

Jan 27 '07 #5


On Jan 27, 10:55 am, "RobG" <r...@iinet.net.auwrote:
On Jan 27, 2:39 am, "Ray" <ray_use...@yahoo.comwrote:
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; }
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).
--
Rob

Jan 27 '07 #6
Ray


On Jan 27, 8:45 am, "RobG" <r...@iinet.net.auwrote:
On Jan 27, 2:22 am, "Ray" <ray_use...@yahoo.comwrote:
Hello,
What do you think about emulating private variables for a class this
way?Try Richard Cornford's article on private static members:

<URL:http://www.litotes.demon.co.uk/js_info/private_static.html>
Thanks Rob,

Cheers,
Ray
--
Rob
Jan 27 '07 #7
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 !-)
Jan 30 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

28
by: Anthony Williams | last post by:
Good morning, I'm currently designing a site, using CSS, and wish to create a variable width two-column layout, with header and footer, and one fixed-width column on the left. Previously, I...
2
by: Bart | last post by:
Hi there, Since you've all told me that frames ar evil, I'm planning to disguard frames in favour of CSS "pseudo-frames" for my personal website. While trying to "emulate frames" (that is I...
21
by: Anthony England | last post by:
Everyone knows that global variables get re-set in an mdb when an un-handled error is encountered, but it seems that this also happens when the variable is defined as private at form-level. So...
2
by: Mike Orb | last post by:
This is a Framework 1.x question. I'm creating a class that generates HTML element IDs (<foo id='id1'> for example) and need the element IDs to be unique across the page. The reusable class...
11
by: prefersgolfing | last post by:
I'm trying to find on MSDN, or someplace, that speaks to variables being public or private by default. Anyone know where? Thanks.
86
by: jopperdepopper | last post by:
Hi, finally giving php 5 a go, and going over the new approach to classes. Can someone clarify the public, private and protected to me? I quote the php manual: "The visibility of a property or...
63
by: time.swift | last post by:
Coming from a C++ / C# background, the lack of emphasis on private data seems weird to me. I've often found wrapping private data useful to prevent bugs and enforce error checking.. It appears...
4
by: Wolfgang Draxinger | last post by:
If you know languages like Python or D you know, that nested functions can be really handy. Though some compilers (looking at GCC) provide the extension of nested functions, I wonder, how one...
21
by: Aaron Gray | last post by:
(Please tell me if this is silly or I am barking up the wrong tree) Can someone check this please on pre W3C DOM browsers :- function getDocumentRoot() { for ( e in document.childNodes) if (...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.