By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,909 Members | 1,285 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,909 IT Pros & Developers. It's quick & easy.

Accessing private members of a different scope in the same type

P: n/a
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(privateKey)
{
this.process = function(someObj)
{
if (!(someObj instanceof SomeObj))
{
// throw an error...
}

// someObj.privateKey is in a different private scope
// and is always undefined.
privateKey = ((privateKey - someObj.privateKey) << 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(privateKey)
{
function getPrivateKey()
{
return privateKey;
}

this.process = function(someObj)
{
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.apply(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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
On Jun 26, 2:14*pm, RobG <rg...@iinet.net.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.com/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 discussion thread is closed

Replies have been disabled for this discussion.