Public static method accessing private method

Discussion in 'Javascript' started by rognon@gmail.com, Aug 3, 2006.

  1. Guest

    Hi there,
    I'm trying to do something, but I don't know if it's possible.
    Basically, I want to have a public static class method that could
    access a private object's method. I would like to be able to do :

    Class.method(InstanceOfClass);

    The method would then access a private function from Class by doing
    something like

    function method(param) {
    param.privateMethodOfClass();
    }

    I've done a lot research and experimentations but just can't come up
    with a solution... I don't even know if what I'm trying to do is
    possible.

    Any help would be appreciated.
    Thanks.
    , Aug 3, 2006
    #1
    1. Advertising

  2. wrote:


    > I'm trying to do something, but I don't know if it's possible.
    > Basically, I want to have a public static class method that could
    > access a private object's method. I would like to be able to do :
    >
    > Class.method(InstanceOfClass);
    >
    > The method would then access a private function from Class by doing
    > something like
    >
    > function method(param) {
    > param.privateMethodOfClass();
    > }


    It depends on how you construct your "private" methods in JavaScript but
    with the usual local function in the constructor function approach you
    could never use the dot notation (e.g. param.method) at all to access
    the local function as the local function is not a property of the object.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Aug 3, 2006
    #2
    1. Advertising

  3. Guest

    Martin Honnen wrote:
    >
    > It depends on how you construct your "private" methods in JavaScript but
    > with the usual local function in the constructor function approach you
    > could never use the dot notation (e.g. param.method) at all to access
    > the local function as the local function is not a property of the object.
    >


    Then how shoud I construct my private method to be able to do what I
    want to do ?
    , Aug 3, 2006
    #3
  4. On 03/08/2006 17:16, wrote:

    [snip]

    > Basically, I want to have a public static class method that could
    > access a private object's method.


    If the 'private' method is defined by a function declaration within the
    body of a constructor function:

    function MyObject() {
    function myPrivateMethod() {
    }
    }

    then that is not possible: the identifier of the former is a property of
    the variable object of the constructor function, not the function itself
    nor the resulting object. That is, it's of local scope. The only way to
    access such 'private' methods is through privileged functions; those
    that are defined within the enclosing execution context, but then
    explicitly exposed:

    function MyObject() {
    function myPrivateMethod() {
    }

    this.myPrivilegedMethod = function() {
    // myPrivateMethod()
    };
    }

    The simplest solution is to make the 'private' method private by convention:

    function MyObject() {
    /* Do NOT call this function directly! */
    this._myPrivateMethod = function() {
    };
    }

    though it may be better to add the method to the prototype object if is
    has no need to access other 'private' data.

    Mike


    It may be possible to take an overly convoluted approach using shared
    secrets, but this is unlikely to be worth the effort.
    Michael Winter, Aug 3, 2006
    #4
  5. Guest

    Michael Winter wrote:

    > The simplest solution is to make the 'private' method private by convention:


    Ok...
    I was hoping that there would be a more secure way of doing this but it
    seems I'll have to rely on conventions.

    Thanks anyway
    , Aug 3, 2006
    #5
    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. Charles A. Lackman
    Replies:
    1
    Views:
    1,341
    smith
    Dec 8, 2004
  2. SpamProof
    Replies:
    0
    Views:
    547
    SpamProof
    Oct 21, 2003
  3. qazmlp
    Replies:
    19
    Views:
    787
    Daniel T.
    Feb 4, 2004
  4. DaveLessnau
    Replies:
    3
    Views:
    424
    Howard
    May 16, 2005
  5. earthwormgaz
    Replies:
    6
    Views:
    451
    earthwormgaz
    Jun 1, 2010
Loading...

Share This Page