Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Public static method accessing private method

Reply
Thread Tools

Public static method accessing private method

 
 
rognon@gmail.com
Guest
Posts: n/a
 
      08-03-2006
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.

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      08-03-2006


http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
rognon@gmail.com
Guest
Posts: n/a
 
      08-03-2006
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 ?

 
Reply With Quote
 
Michael Winter
Guest
Posts: n/a
 
      08-03-2006
On 03/08/2006 17:16, (E-Mail 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.
 
Reply With Quote
 
rognon@gmail.com
Guest
Posts: n/a
 
      08-03-2006
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSE 4 11-15-2006 02:40 AM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola Microsoft Certification 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd loyola MCSD 3 11-14-2006 05:18 PM
microsoft.public.certification, microsoft.public.cert.exam.mcsa, microsoft.public.cert.exam.mcad, microsoft.public.cert.exam.mcse, microsoft.public.cert.exam.mcsd realexxams@yahoo.com Microsoft Certification 0 05-10-2006 02:35 PM
microsoft.public.dotnet.faqs,microsoft.public.dotnet.framework,microsoft.public.dotnet.framework.windowsforms,microsoft.public.dotnet.general,microsoft.public.dotnet.languages.vb Charles A. Lackman ASP .Net 1 12-08-2004 07:08 PM



Advertisments