Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Private methods called from public methods

Reply
Thread Tools

Private methods called from public methods

 
 
Robert
Guest
Posts: n/a
 
      05-03-2006
Hi,
I have been reading the article at
http://www.crockford.com/javascript/private.html
and I was wondering if there also was some way to be able to have
private methods that can be called from public methods, but not from
outside.
Just throwing an exception if a private method was called from outside
would be sufficient, but I don't know how to determine if the method was
called from outside. Anyone else thought about doing this?

Robert.
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      05-03-2006

Robert wrote:
> I was wondering if there also was some way to be able to have
> private methods that can be called from public methods, but not from
> outside.


A bit strange question. The whole idea of private methods is that they
*cannot* be called outside. If you can call a method from outside, then
it is not private.

Could you post some code or an object structure you're trying to
achieve?

 
Reply With Quote
 
 
 
 
Robert
Guest
Posts: n/a
 
      05-03-2006
VK wrote:
> Robert wrote:
>
>>I was wondering if there also was some way to be able to have
>>private methods that can be called from public methods, but not from
>>outside.

>
>
> A bit strange question. The whole idea of private methods is that they
> *cannot* be called outside. If you can call a method from outside, then
> it is not private.


Of course I would prefer that the private method could not be called,
but I would be satisfied with an error.

> Could you post some code or an object structure you're trying to
> achieve?
>


The code I am thinking of usual public method for black-box
functionality and private methods for internal support functions.

For example:

function List()
{
this.array = new Array();
}
//public
List.prototype.insert = insert;
//private
List.prototype.shiftRight = shiftRight;

function insert(obj, index)
{
this.shiftRight(index);
this.array[index] = obj;
}

function shiftRight(index)
{
// move array elements starting at index one to the right
}

The "solution" at http://www.crockford.com/javascript/private.html would
mean that I have to move everything inside the List constructor.
Insert must become a privileged method and shiftRight a private method.
I prefer not having to move everything inside to keep the code more
legible. If only a method could detect if the call came from in or
outside then I would be satisfied.

Robert.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      05-03-2006

Robert wrote:
> The code I am thinking of usual public method for black-box
> functionality and private methods for internal support functions.


This is the way I'm using (please not I'm not a real OOP expert of any
kind):

<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type"
content="text/html; charset=iso-8859-1">
<script>
function List(arr) {
this.array = arr || [];
this.count = List.count;
}
List.count = function() {
if (this instanceof List) {
return this.array.length;
}
else {
throw new Error('Private method call ourside of scope');
}
}

var obj = new List([1,2,3]);
try {
alert( obj.count() );
alert( List.count() );
}
catch(e) {
alert(e.message);
}
</script>
</head>

<body>

</body>
</html>


There are other ways different in approaches and needed amount of
different brackets. I'm using this one as - IMHO - the simpliest one as
it doesn't require to twist your brains every second by trying to
figure out what "this" is pointing to in this or that place.

The drawback of this way is that JScript.Net in quick mode doesn't
allow Function object augmentation. So if you plan to port some your
solutions on JScript.Net, you may better use something else. From the
other side JScript.Net has regular "public" and "private" modifiers, so
the problem as such is not applicable there.

 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      05-03-2006
VK wrote:
> Robert wrote:
>
>>The code I am thinking of usual public method for black-box
>>functionality and private methods for internal support functions.

>
>
> This is the way I'm using (please not I'm not a real OOP expert of any
> kind):
>
> function List(arr) {
> this.array = arr || [];
> this.count = List.count;
> }
> List.count = function() {
> if (this instanceof List) {
> return this.array.length;
> }
> else {
> throw new Error('Private method call ourside of scope');
> }
> }


This checks if it is called AS an instance or static method.
It does not check if it is called FROM a list instance method.

In my example this in the shiftRight method would be an instance of
List, because it is called from the insert method.
 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      05-03-2006
Robert wrote:
> VK wrote:
>
>> Robert wrote:
>>
>>> The code I am thinking of usual public method for black-box
>>> functionality and private methods for internal support functions.

>>
>>
>>
>> This is the way I'm using (please not I'm not a real OOP expert of any
>> kind):
>>
>> function List(arr) {
>> this.array = arr || [];
>> this.count = List.count;
>> }
>> List.count = function() {
>> if (this instanceof List) {
>> return this.array.length;
>> }
>> else {
>> throw new Error('Private method call ourside of scope');
>> }
>> }

>
>
> This checks if it is called AS an instance or static method.
> It does not check if it is called FROM a list instance method.
>
> In my example this in the shiftRight method would be an instance of
> List, because it is called from the insert method.


And if it's called from outside as list.shiftRight() then this would
still be and instance of List..

I forgot to add this.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      05-03-2006

Robert wrote:
> Robert wrote:
> > VK wrote:
> >
> >> Robert wrote:
> >>
> >>> The code I am thinking of usual public method for black-box
> >>> functionality and private methods for internal support functions.
> >>
> >>
> >>
> >> This is the way I'm using (please not I'm not a real OOP expert of any
> >> kind):
> >>
> >> function List(arr) {
> >> this.array = arr || [];
> >> this.count = List.count;
> >> }
> >> List.count = function() {
> >> if (this instanceof List) {
> >> return this.array.length;
> >> }
> >> else {
> >> throw new Error('Private method call ourside of scope');
> >> }
> >> }

> >
> >
> > This checks if it is called AS an instance or static method.
> > It does not check if it is called FROM a list instance method.
> >
> > In my example this in the shiftRight method would be an instance of
> > List, because it is called from the insert method.

>
> And if it's called from outside as list.shiftRight() then this would
> still be and instance of List..
>
> I forgot to add this.

Program in C++ and use public/private/protected modifiers as descent
people do
Seriously: JavaScript doesn't have such modifiers, so it has to be
severely hacked to get anything close to what you get by a single
modifier in say Java.

Here is the most popular (AFAIK) way:
<http://www.litotes.demon.co.uk/js_info/private_static.html#mProt>

P.S. I keep promising to myself to eventually understand what and how
are they doing - but just losing it on the 2nd pair of brackets


 
Reply With Quote
 
Robert
Guest
Posts: n/a
 
      05-03-2006
VK wrote:
> Program in C++ and use public/private/protected modifiers as descent
> people do
> Seriously: JavaScript doesn't have such modifiers, so it has to be
> severely hacked to get anything close to what you get by a single
> modifier in say Java.
>
> Here is the most popular (AFAIK) way:
> <http://www.litotes.demon.co.uk/js_info/private_static.html#mProt>
>


Thanks. I'll try to read it soon.
And if all else fails I will just assume that 'private' methods are not
called by using a convention such as clearly stating they are private
and using a different syntax for the function names.
 
Reply With Quote
 
VK
Guest
Posts: n/a
 
      05-03-2006

Robert wrote:
> And if all else fails I will just assume that 'private' methods are not
> called by using a convention such as clearly stating they are private
> and using a different syntax for the function names.


Here's the voice of a Man Who Got the Life Right!

 
Reply With Quote
 
RobG
Guest
Posts: n/a
 
      05-03-2006
Robert wrote:
> Hi,
> I have been reading the article at
> http://www.crockford.com/javascript/private.html
> and I was wondering if there also was some way to be able to have
> private methods that can be called from public methods, but not from
> outside.


A method that can call a private method is called 'privileged'. From
memory, the article provides an example of a private & privileged
functions like:


var aFn = (function(){
function privateMethod(x){
alert('privateMethod called with: ' + x);
};

return {
privilegedMethod : function(x){
privateMethod(x);
}
};
})();

aFn.privilegedMethod('blah');


JavaScript is an object-based language, it isn't fully OO like C++. You
can't declare a method as private, you can only infer it from the code
structure as above.


> Just throwing an exception if a private method was called from outside
> would be sufficient, but I don't know how to determine if the method was
> called from outside. Anyone else thought about doing this?


Make the method so it can't be called from outside and you're done.


--
Rob
 
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