473,799 Members | 2,926 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(In stanceOfClass);

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

function method(param) {
param.privateMe thodOfClass();
}

I've done a lot research and experimentation s 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 5790


ro****@gmail.co m 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(In stanceOfClass);

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

function method(param) {
param.privateMe thodOfClass();
}
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.co m 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.myPrivileg edMethod = function() {
// myPrivateMethod ()
};
}

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

function MyObject() {
/* Do NOT call this function directly! */
this._myPrivate Method = 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form...
6
15706
by: RCAJ | last post by:
How can I do the following? class C{ private: static const char c = {1,2,3}; //... }; The compiler says it can't be done like that.
6
8307
by: GG | last post by:
Is this public static method thread safe. //Receives a file name as a parameter //and returns the contents of that file as a string public static string FileToStr(string fileName) { FileStream fStream=null; lock(fStream) //just in case we use it for multithreading to be thread safe {
6
3592
by: z_learning_tester | last post by:
Quick question- What happens if you have a private class with a public static method? Can you still say the following? Lets say you are making this call from another class, say class2... int myVal = class1.method(); It seems that you should not be able to, after all from class2, you should not be able to see the private class1, so it's public static method is effectively wasted.
2
2059
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking afterwards with ILDASM at what is visible in those assemblies from a managed point-of-view I've noticed that: 1) for each managed and unmanaged C function (not C++ classes) I get a public managed static method (defined on a 'Global Functions' class) in the generated assembly with an export name of the form
8
2022
by: Al | last post by:
I'd like to create Class Library in VB 2005, which has a property accessible by external programs. I decided to include 1 Class with 1 property in this project. I placed this code in Class: Public Class COM Private mMyProp As String Public Property MyProp() As String
2
1080
by: seanjones | last post by:
I'm building a simple C# Win App to monitor some directories and then kick of some processes - when files hit the system. Right now everything works except that i have a listView control on a form and when i click the button to tell the app to start monitoring. I cannot get the "addItems()" method to update the Forms listView - it won't even compile. Any Ideas
6
14208
by: Andrus | last post by:
class Class1 { public void foo() {} } Does not cause any error or warning at compile time. Why ? public access modifier should not allowed in foo() since it is member of
11
2443
by: TinaJones095 | last post by:
Hello I am going to give a program that I have done, but I have to modifiy it, but I need help okay can you help ? Here the program I need help to straighten up below: the Java error is right at the point is at the end of the program. //week 6 Inventory3 // by Tina Jones // IT 215 // october 07/2007
0
9687
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9541
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10251
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10027
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7564
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6805
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.