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