Hi,

> > var myObject = new femalePatient(37);
> >
> > alert(myObject.age); // alerts 42
>
> This shows the OOP limitations of Javascript. In a real-world femalePatient
> object, the age property would be unavailable. ;-)

OK, Dave, Jörn and Dan to sattisfy all your needs:

function Patient(age) {
        // age is not world readable any more for Dave

        // geter and setter functions for Jörn
        this.getAge = function() { return age; };
        this.setAge = function(newage) { age = newage; };
}
function femalePatientBase(shoesize) {
        Patient.apply(this,[42]);
        this.shoesize = shoesize;
}
femalePatient = ( $user == 'Dan' ) ? function(shoesize) {
        femalePatientBase.apply(this,[shoesize]);
        // Dan wants the age divided by 2
        var oldgetage = this.getAge;
        this.getAge = function() { return oldgetage.apply(this,[])/2; };
} : function(shoesize) {
        femalePatientBase.apply(this,[shoesize]);
        // Jörn wants an exception here
        this.getAge = function() { throw('Age not available for female 
Patients'); };
};
femalePatient.prototype = new Patient(0);

var myObject = new femalePatient(37);
alert(myObject.age); // alerts 21 if $user was 'Dan', Throws an exception 
                     // otherwise

Now where is the limitation of JavaScript? ;-)

An object model that clearly reflects the reality would need mutliple 
inheritance here, because the considerations about the age apply to females 
that are not patients as well of course.

Christof

_______________________________________________
jQuery mailing list
[email protected]
http://jquery.com/discuss/

Reply via email to