Public static method accessing private method

R

rognon

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

Martin Honnen

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

rognon

Martin said:
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 ?
 
M

Michael Winter

On 03/08/2006 17:16, (e-mail address removed) 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.
 
R

rognon

Michael said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top