Accessing private members of a different scope in the same type

Discussion in 'Javascript' started by agendum97@gmail.com, Jun 26, 2008.

  1. Guest

    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 26, 2008
    #1
    1. Advertising

  2. RobG Guest

    On Jun 27, 4:38 am, 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
    RobG, Jun 26, 2008
    #2
    1. Advertising

  3. Guest

    On Jun 26, 2:14 pm, RobG <> wrote:
    > On Jun 27, 4:38 am, 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 26, 2008
    #3
  4. 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.'
    Lasse Reichstein Nielsen, Jun 26, 2008
    #4
  5. <> 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.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.
    Richard Cornford, Jun 26, 2008
    #5
  6. Guest

    On Jun 26, 3:53 pm, "Richard Cornford" <>
    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 === 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
    , Jun 27, 2008
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. CoolPint
    Replies:
    8
    Views:
    948
    Jeff Schwab
    Dec 14, 2003
  2. Hans De Winter
    Replies:
    4
    Views:
    329
    Sumit Rajan
    Jan 17, 2004
  3. Sandeep

    Accessing private members

    Sandeep, Aug 1, 2004, in forum: C++
    Replies:
    5
    Views:
    379
    Karl Heinz Buchegger
    Aug 3, 2004
  4. James A
    Replies:
    7
    Views:
    244
    Flash Gordon
    Sep 17, 2004
  5. Hicham Mouline
    Replies:
    5
    Views:
    2,338
    James Kanze
    Dec 19, 2008
Loading...

Share This Page