473,881 Members | 1,561 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Accessing private members of a different scope in the same type

Is there a way of constructing a "class" in JavaScript to access a
private member of a different scope of the same type? It is important
that the variable remain private, and not accessible in anyway
publically. For example:

function SomeObj(private Key)
{
this.process = function(someOb j)
{
if (!(someObj instanceof SomeObj))
{
// throw an error...
}

// someObj.private Key is in a different private scope
// and is always undefined.
privateKey = ((privateKey - someObj.private Key) << 1) % 500;
}

// some privateKey validation here...
}

Note, this sample above is just a sample to demonstrate what I am
asking, it is not real code. Here I need privateKey to remain
completely private -- with exception to members of the same type. So
I thought about doing this:

function SomeObj(private Key)
{
function getPrivateKey()
{
return privateKey;
}

this.process = function(someOb j)
{
if (!(someObj instanceof SomeObj))
{
// throw an error...
}

// Even though I am changing the scope to someObj here,
// the value returned by getPrivateKey is still the
// local one.
privateKey = ((privateKey - getPrivateKey.a pply(someObj)) <<
1) % 500;
}

// some privateKey validation here...
}

This too doesn't work (though it seems like it should...). The only
thing I've come up with is keeping track of my objects in a private
static and referring to them later. Though I know this is horrible
because it prevents objects from being garbage collected.

Does anybody have any other ideas how to accomplish this in JavaScript
without making a public this.method for accessing the privateKey?

Thanks
Jun 27 '08 #1
2 1611
On Jun 27, 4:38*am, agendu...@gmail .com wrote:
Is there a way of constructing a "class" in JavaScript to access a
private member of a different scope of the same type? *It is important
that the variable remain private, and not accessible in anyway
publically.
Have you read Douglas Crockford's article on Private Members in
JavaScript?

<URL: http://www.crockford.com/javascript/private.html >
--
Rob
Jun 27 '08 #2
On Jun 26, 2:14*pm, RobG <rg...@iinet.ne t.auwrote:
On Jun 27, 4:38*am, agendu...@gmail .com wrote:
Is there a way of constructing a "class" in JavaScript to access a
private member of a different scope of the same type? *It is important
that the variable remain private, and not accessible in anyway
publically.

Have you read Douglas Crockford's article on Private Members in
JavaScript?

<URL:http://www.crockford.c om/javascript/private.html>

--
Rob
Yes... and that is exactly what I am doing above. privateKey is a
private variable. I am still trying to figure out how to construct a
"class" which solves the problem in my question above.
Jun 27 '08 #3

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

Similar topics

5
3661
by: Suzanne Vogel | last post by:
Hi, Given: I have a class with protected or private data members, some of them without accessor methods. It's someone else's class, so I can't change it. (eg, I can't add accessor methods to the parent class, and I can't make some "helper" class a friend of the parent class to help in accessing the data.) Problem: I want to derive a class that has a copy constructor that properly copies those data members.
6
2183
by: Chris Mantoulidis | last post by:
Forgive me if I'm wrong but I think there is something like an extra member scope in classes. for example: class abc { ostream & operator << (ostream &, const abc &); istream & operator >> (istream &, abc &); private:
5
2421
by: Sandeep | last post by:
Hi, In the following code, I wonder how a private member of the class is being accessed. The code compiles well in Visual Studio 6.0. class Sample { private: int x; public:
8
7835
by: __PPS__ | last post by:
Hello everybody, today I had another quiz question "if class X is privately derived from base class Y what is the scope of the public, protected, private members of Y will be in class X" By scope they meant public/protected/private access modifiers :) Obviously all members of the base privately inherited class will be private, and that was my answer. However, the teacher checked my answers when I handed in, and the problem was that I had...
6
2631
by: harry | last post by:
Hi ppl I have a question about memory layout of a class. Consider the code below: class Base1 { virtual void f() { cout << "Base1::f" << endl; } virtual void g() { cout << "Base1::g" << endl; } }; class Drive : public Base1{
6
7769
by: Carlos | last post by:
Hi all, I am trying to access a public field of another form class within the same namespace. The field is public, what is the best way to access it from a different class? I defined as private MyNameSpace.Form1 cForm1; and I am trying to use it later as TextBox.Text = cForm1.TextBox.Text;
23
2620
by: Ben Voigt | last post by:
I have a POD type with a private destructor. There are a whole hierarchy of derived POD types, all meant to be freed using a public member function Destroy in the base class. I get warning C4624. I read the description, decided that it's exactly what I want, and ignored the warning. Now I'm trying to inherit using a template. Instead of "destructor could not be generated because a base class destructor is inaccessible", I now have an...
6
5039
by: earthwormgaz | last post by:
Is the following legal? class Outer { class Inner { private: Inner() { } };
9
4696
by: Matthias Buelow | last post by:
Hi folks, I've got something like: class Outer { int f(); friend class Inner; class Inner { int g() {
0
11098
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10814
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10401
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9552
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7953
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7109
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4597
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4196
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.