Accessing private members of a different scope in the same type

A

agendum97

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
 
L

Lasse Reichstein Nielsen

Is there a way of constructing a "class" in JavaScript to access a
private member of a different scope of the same type?

If I understand your question correctly (and that's by no means
certain), then the answer is no.

There is nothing in Javascript that distinguishes one object's "class"
from another (since there are no classes), so there is nothing that an
object of one type can do to another object, that one of another type
can't.
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.

You want each instance of your type to have a value associated, that
another instance can access, but that objects of other types can't.

Your solution, to keep one mapping from instance to associated value,
that all instances have access to through the constructor, is the only
one I can think of. And it's not pretty (and hard to do, since objects
can't be used directly as keys for a map).
Does anybody have any other ideas how to accomplish this in JavaScript
without making a public this.method for accessing the privateKey?

No.
Javascript doesn't have access control. Trying to impose it will only
make your code convoluted and fragile.
Just document that nobody should fiddle with the value, and let others
shoot themselves in the foot if they really want to.

If this is client-side scripts, any determined attacker can get the
private key anyway, since he has complete control of the client.

/L
 
R

Richard Cornford

On Jun 26, 2:14 pm, RobG wrote:
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.

Do what Bjoern Hoedrmann suggested and read my article via:-

<URL:
http://web.archive.org/*/http://www.litotes.demon.co.uk/js_info/private_static.htmll >

- and particularly the Protected Instance Members in Javascript section.
That is as near as you are going to get. But remember that the code was
written to demonstrate a theoretical possibility and (to the best of my
knowledge nobody has ever considered actually doing that in a real world
application.

(The hosts of www.litotes.demon.co.uk have done something irritating to
it and it will probably take a couple of days to get it fixed.)

Richard.
 
A

agendum97

Do what Bjoern Hoedrmann suggested and read my article via:-

<URL:http://web.archive.org/*/http://www.litotes.demon.co.uk/js_info/priva...>

Thanks for the reference to that article. I've studied the article
quite a bit and appears all you are doing is implicitly keeping track
of all the object references using a function chain. Even so, all
objects are still always referenced (by function bodies) and thus will
never be garbage collected (which is bad). If we are to permit this,
I think a much more simple, and probably much more performant
implementation would be:



var Outer = function()
{
var objectTable = [];
var protoTable = [];

function getProtoTable(obj)
{
for (var i = 0; i < objectTable.length; ++i)
{
if (objectTable === obj)
{
return protoTable;
}
}
}

function ctor(val)
{
objectTable.push(this);
protoTable.push(new Object());

getProtoTable(this).val = val;

this.showOther = function(obj)
{
window.alert(getProtoTable(obj).val);
}
}

return ctor;
}();

var a = new Outer(1);
var b1 = new Outer(2);
var b2 = new Outer(3);
var b3 = new Outer(4);

a.showOther(b1);
a.showOther(b2);
a.showOther(b3);


However... I don't want to do this because the objects should be
garbage collected.

Thanks
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top