Accessing private members of a different scope in the same type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • agendum97@gmail.com

    Accessing private members of a different scope in the same type

    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(private Key)
    {
    this.process = function(someOb j)
    {
    if (!(someObj instanceof SomeObj))
    {
    // throw an error...
    }

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

    this.process = function(someOb j)
    {
    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.a pply(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
  • RobG

    #2
    Re: Accessing private members of a different scope in the same type

    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

    Comment

    • agendum97@gmail.com

      #3
      Re: Accessing private members of a different scope in the same type

      On Jun 26, 2:14 pm, RobG <rg...@iinet.ne t.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.c om/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.

      Comment

      Working...