471,596 Members | 1,218 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,596 software developers and data experts.

Public static method accessing private method

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 '06 #1
4 5671


ro****@gmail.com 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/
Aug 3 '06 #2
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 '06 #3
On 03/08/2006 17:16, ro****@gmail.com 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.
Aug 3 '06 #4
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 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by RCAJ | last post: by
6 posts views Thread by z_learning_tester | last post: by
8 posts views Thread by Al | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.