473,598 Members | 3,150 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 5775


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
15697
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
8298
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
3578
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
2040
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
2012
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
1075
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
14205
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
2430
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
8389
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8263
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...
0
6713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5848
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
5438
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
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2411
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
1
1503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1246
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.