JS Public Class
JS Public Class - all members and methods are public - the function's required parameters work like a constructor to initialize an instance of the class
by BumbleB2na
JavaScript
// Person class
function Person(curFirstName /* string */, curLastName /* string */) {
return {
FirstName: curFirstName,
LastName: curLastName,
FullName: function() {
return( this.FirstName + " " + this.LastName );
}
};
}
// Musician class inherits Person class
function Musician(curInstrument /* string */, curPerson /* object */) {
return {
Parent: curPerson,
Instrument: curInstrument,
Details: function() {
return( this.Parent.FullName() + " plays a " + this.Instrument);
}
};
}
var musician = new Musician('Gibson ES-355', new Person('B.B.', 'King'));
alert(musician.Details());
// You can add on new public members or methods
musician.InstrumentName = 'Lucille';
musician.MoreDetails = function() { return(this.Details() + ' named, ' + this.InstrumentName); };
alert(musician.MoreDetails());