Prototype Inheritance
/**
* Person Definition
*/
var Person = function() {};
Person.prototype.setName = function(name) {
this.name = name;
return this;
}
Person.prototype.getName = function() {
return this.name;
}
/**
* RaceCarDriver Definition
*/
var RaceCarDriver = function() {};
// set prototype to parent
RaceCarDriver.prototype = new Person();
RaceCarDriver.prototype.setSponsor = function(sponsor) {
this.sponsor = sponsor;
return this;
}
RaceCarDriver.prototype.getSponsor = function() {
return this.sponsor;
}
RaceCarDriver.prototype.getDriverInfo = function() {
return this.getName() + ' drives the ' + this.getSponsor() + ' car.';
}
/**
* Application Code
*/
var joe = new RaceCarDriver();
joe.setName('Joseph').setSponsor('Tide');
var output = joe.getDriverInfo();
console.log(output);