Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Accessing private members of a different scope in the same type

Reply
Thread Tools

Accessing private members of a different scope in the same type

 
 
agendum97@gmail.com
Guest
Posts: n/a
 
      06-26-2008
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
 
Reply With Quote
 
 
 
 
RobG
Guest
Posts: n/a
 
      06-26-2008
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
 
Reply With Quote
 
 
 
 
agendum97@gmail.com
Guest
Posts: n/a
 
      06-26-2008
On Jun 26, 2:14*pm, RobG <rg...@iinet.net.au> wrote:
> 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.
 
Reply With Quote
 
Lasse Reichstein Nielsen
Guest
Posts: n/a
 
      06-26-2008
writes:

> 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
--
Lasse Reichstein Nielsen
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
 
Reply With Quote
 
Richard Cornford
Guest
Posts: n/a
 
      06-26-2008
<> wrote:
>On Jun 26, 2:14 pm, RobG wrote:

<snip>
>> Have you read Douglas Crockford's article on Private Members in
>> JavaScript?
>>
>> <URL:http://www.crockford.com/javascript/private.html>

<snip>

> 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....e_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.

 
Reply With Quote
 
agendum97@gmail.com
Guest
Posts: n/a
 
      06-27-2008
On Jun 26, 3:53*pm, "Richard Cornford" <Rich...@litotes.demon.co.uk>
wrote:
> 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[i] === obj)
{
return protoTable[i];
}
}
}

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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can *common* struct-members of 2 different struct-types, that are thesame for the first common members, be accessed via pointer cast to either struct-type? John Reye C Programming 28 05-08-2012 12:24 AM
Thread safety problems with function scope static variables vs class static private members Hicham Mouline C++ 5 12-19-2008 08:10 PM
Accessing members of array as a different type James A C Programming 7 09-17-2004 08:24 AM
Question on friend functions and accessing private members Hans De Winter C++ 4 01-17-2004 08:13 AM
Can nested class members access private members of nesting class? CoolPint C++ 8 12-14-2003 02:30 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57