K
Kevin Prichard
Hi all,
I've recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I'd like to discuss with you experts.
I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of
ways to do this through prototyping and other techniques, but these
behaviors need to be static and protected. By protected this means
that they can be overridden (or reassigned) in subclasses, and
privileged subclass methods can access them, but callers external to
the class cannot.
I've been attempting to define a factory function that transfers its
parameters to an anonymous object while defining the constructor in a
-with- block. (This is what I'm loosely referring to as "subclassing"
- it's really object composition.) This works, but not for
externally-defined functions. They can't see the protected static
members.
// helper functions to be passed into factory function, for protected
static use
var ProtStaticMethod1 = function(that) { // caller passes in 'this'
// call privileged bridge function to access protected static value
alert("PSM1: "+that.GetPSField());
};
var ProtStaticMethod2 = function(that) {
alert("PSM2: "+ProtStaticField);
};
// helper function, to be accessed as a privileged class method
var candidateStaticMethod = function() {
alert("PrivOutsiderSM: "+
(typeof(ProtStaticField)=="undefined" ?
"Outsider static method can't find ProtStaticField" : ProtStaticField)
);
};
// factory function; params: 1) function, 2) any data
var BaseFactory = function(psmMethod,psmField,privilegedMeth) {
with ({
ProtStaticField : psmField,
ProtStaticMethod : psmMethod
})
{
function Constructor() { // instance members just for fun
var privInstMember = "privateMember";
function privInstMethod() { alert(privInstMember); };
};
Constructor.prototype.ShowPSField = function() {
alert("ShowPSField: "+ProtStaticField); }
Constructor.prototype.InvokePSMethod = function() {
ProtStaticMethod(this); }
Constructor.prototype.SetPSField = function(val) {
ProtStaticField=val; }
Constructor.prototype.GetPSField = function() { return
ProtStaticField; }
Constructor.PrivOutsideMethod = privilegedMeth;
Constructor.PrivInsideMethod = function() {
alert("PrivInsiderSM: "+
(typeof(ProtStaticField)=="undefined" ?
"Can't find ProtStaticField" : ProtStaticField) );
};
};
return Constructor;
};
SubClass1 = BaseFactory( ProtStaticMethod1, "ProtStaticField_Class1",
candidateStaticMethod );
// can a function defined outide the constructor's scope see privileged
static fields?
SubClass1.PrivOutsideMethod();
SubClass2 = BaseFactory( ProtStaticMethod2, "ProtStaticField_Class2",
candidateStaticMethod );
// can a privileged static constructor method see protected static
fields?
SubClass1.PrivInsideMethod();
obj1 = new SubClass1();
obj2 = new SubClass2();
obj1.ShowPSField(); // should display "ProtStaticField_Class1"
obj2.ShowPSField(); // should display "ProtStaticField_Class2"
obj1.InvokePSMethod(); // should display "ProtStaticField_Class1"
obj2.InvokePSMethod(); // CRASH - ProtStaticMethod2 can't see
ProtStaticField
This has worked for producing subclasses whose protected static fields
are composed of just about any values.
BUT, my chief complaint is that functions defined outside the scope of
the factory's -with- block aren't able to see the protected static
members. Is that just a limitation of this technique, or is there
something I need to change? The only thing that has worked at all is,
as you can see, to add privileged bridge functions to the constructor's
prototype. These act as a go-between for the externally defined
functions stored, e.g. GetPSField as accessed by external function
ProtStaticMethod1.
Is there a better approach for producing "subclasses" whose protected
static methods can be varied from outside, yet still access protected
members?
Thanks in advance for any feedback,
Kevin Prichard
I've recently been following the object-oriented techiques discussed
here and have been testing them for use in a web application. There is
problem that I'd like to discuss with you experts.
I would like to produce Javascript classes that can be "subclassed"
with certain behaviors defined at subclass time. There are plenty of
ways to do this through prototyping and other techniques, but these
behaviors need to be static and protected. By protected this means
that they can be overridden (or reassigned) in subclasses, and
privileged subclass methods can access them, but callers external to
the class cannot.
I've been attempting to define a factory function that transfers its
parameters to an anonymous object while defining the constructor in a
-with- block. (This is what I'm loosely referring to as "subclassing"
- it's really object composition.) This works, but not for
externally-defined functions. They can't see the protected static
members.
// helper functions to be passed into factory function, for protected
static use
var ProtStaticMethod1 = function(that) { // caller passes in 'this'
// call privileged bridge function to access protected static value
alert("PSM1: "+that.GetPSField());
};
var ProtStaticMethod2 = function(that) {
alert("PSM2: "+ProtStaticField);
};
// helper function, to be accessed as a privileged class method
var candidateStaticMethod = function() {
alert("PrivOutsiderSM: "+
(typeof(ProtStaticField)=="undefined" ?
"Outsider static method can't find ProtStaticField" : ProtStaticField)
);
};
// factory function; params: 1) function, 2) any data
var BaseFactory = function(psmMethod,psmField,privilegedMeth) {
with ({
ProtStaticField : psmField,
ProtStaticMethod : psmMethod
})
{
function Constructor() { // instance members just for fun
var privInstMember = "privateMember";
function privInstMethod() { alert(privInstMember); };
};
Constructor.prototype.ShowPSField = function() {
alert("ShowPSField: "+ProtStaticField); }
Constructor.prototype.InvokePSMethod = function() {
ProtStaticMethod(this); }
Constructor.prototype.SetPSField = function(val) {
ProtStaticField=val; }
Constructor.prototype.GetPSField = function() { return
ProtStaticField; }
Constructor.PrivOutsideMethod = privilegedMeth;
Constructor.PrivInsideMethod = function() {
alert("PrivInsiderSM: "+
(typeof(ProtStaticField)=="undefined" ?
"Can't find ProtStaticField" : ProtStaticField) );
};
};
return Constructor;
};
SubClass1 = BaseFactory( ProtStaticMethod1, "ProtStaticField_Class1",
candidateStaticMethod );
// can a function defined outide the constructor's scope see privileged
static fields?
SubClass1.PrivOutsideMethod();
SubClass2 = BaseFactory( ProtStaticMethod2, "ProtStaticField_Class2",
candidateStaticMethod );
// can a privileged static constructor method see protected static
fields?
SubClass1.PrivInsideMethod();
obj1 = new SubClass1();
obj2 = new SubClass2();
obj1.ShowPSField(); // should display "ProtStaticField_Class1"
obj2.ShowPSField(); // should display "ProtStaticField_Class2"
obj1.InvokePSMethod(); // should display "ProtStaticField_Class1"
obj2.InvokePSMethod(); // CRASH - ProtStaticMethod2 can't see
ProtStaticField
This has worked for producing subclasses whose protected static fields
are composed of just about any values.
BUT, my chief complaint is that functions defined outside the scope of
the factory's -with- block aren't able to see the protected static
members. Is that just a limitation of this technique, or is there
something I need to change? The only thing that has worked at all is,
as you can see, to add privileged bridge functions to the constructor's
prototype. These act as a go-between for the externally defined
functions stored, e.g. GetPSField as accessed by external function
ProtStaticMethod1.
Is there a better approach for producing "subclasses" whose protected
static methods can be varied from outside, yet still access protected
members?
Thanks in advance for any feedback,
Kevin Prichard