Re: Protected static members, abstract classes, object composition vs. subclassing
- From: "VK" <schools_ring@xxxxxxxxx>
- Date: 28 Nov 2005 03:08:46 -0800
Richard Cornford wrote:
> try {
> var goodBoy = new myClass();
> goodBoy.messenger('Hi!');
> }
> catch(e) {
> alert(e.message);
> }
>
> try {
> goodBoy.messenger.call({utils:goodBoy.utils}, 'Hi xxx!');
> goodBoy.utils.messenger('Hi yyy xxx!');
> ({messenger:goodBoy.utils.messenger}).messenger('Hi mmm xxx!');
> }
> catch(e) {
> alert(e.message);
> }
Actually... having read you post once again I'm missing the point of
your exaltation.
myClassUtils constructor is minded to be "protected" - not "private".
These are all different categories (depending of course of the school).
For me "protected" means that I cannot obtain an instance of such class
independently but only as a part of a super-imposed class (myClass in
this case).
So I don't see how the use of goodBoy which is a legal instance of
myClass may prove anything in the concern of the "protected" status.
"private" means that a particular member is accessible only to the
privileged object members - in the most common case to public object
members. So in the listed case we want to make
myClass.utils.messenger() to be accessible only to members of myClass.
It is more tricky because despite argument.callee.caller gives us the
actual caller, we cannot simply ask to caller "Who's your master?"
because the caller has no clue itself at that time ("incontinence of
[this]") But you can check against some caller's feature to see if it's
a valid caller. I left the final solution open so the relevant block is
commented out. The first idea would be to use Java's object
id-stamping. Thus each new instance get's an unique ID and you stamp
all members which this ID:
this.foo = /*public*/ function() {};
this.foo.oID = newID;
this.bar = /*private*/ function() {};
this.bar.oID = newID;
and then later in bar:
if (arguments.callee.caller.oID = (arguments.callee.oID) {
// legal call
}
else {
throw new Error("Attempt to access private member out of the object
scope");
}
I'm not saying this is a final solution and in any case it still
doesn't guarantee a buletproof protection as I said in the original
post.
.
- Follow-Ups:
- References:
- Prev by Date: Re: How to modify this script to allow negative numbers??
- Next by Date: Re: XmlHttpRequest not loading latest version of xml
- Previous by thread: Re: Protected static members, abstract classes, object composition vs. subclassing
- Next by thread: Re: Protected static members, abstract classes, object composition vs. subclassing
- Index(es):
Relevant Pages
|