
The object inheritance above is taken from Mozilla's Tutorial.
When we declare a new person of type Engineer it inherits from Employee, and Workerbee and we declare it as:
var Mark = new Engineer()
However, I have some code wherein I need to create an Engineer using this pattern:
var Mark = new Employee(id).WorkerBee(project).Engineer();
Could someone please point me to an example to make this work? Below is my implementation
function Employee(id) {
//variables
this.id = id
this.name = "";
this.dept = "general";
//methods
this.getId = function (){
return this.id
}
}
Employee.prototype.WorkerBee = WorkerBee;
Employee.prototype.Engineer = Engineer
function WorkerBee(project) {
//variables
this.projectName = project
this.projects = [];
//methods
this.getProjectName = function (){
return this.projectName
}
return this
}
WorkerBee.prototype.Engineer = Engineer
function Engineer() {
//variables
this.dept = "engineering";
this.machine = "";
//methods
this.getDept = function(){
return this.dept
}
return this
}
var Mark = new Employee("5").WorkerBee("Secret Project").Engineer();
console.log(Mark.getId()) //prints 5
console.log(Mark.getProjectName()) //prints Secret Project
console.log(Mark.getDept()) //prints engineering
WorkerBee's prototype depends onEngineer, alsoEngineer's prototype depends onWorkerBee?new Employee()returns a plain Object, an instance of Employee. It doesn't have a WorkerBee method.