Reputation: 1637
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
Upvotes: 0
Views: 139
Reputation:
I'm assuming your question is about how to set things up so your proposed syntax can work exactly as you specified:
var Mark = new Employee(id).WorkerBee(project).Engineer();
So this post is not about the philosophy of inheritance, whether JS does or not have classes, or is classical inheritance a good thing, or hey you should use class system XYZ, or anything else, but specifically about how to achieve your syntax.
In a nutshell, your syntax is going to be tricky to implement if you try to adhere to "classical" inheritance. Your Employee
constructor, when called with new
, is going to need to return something, but at that point it knows nothing about what the object is going to end up being. Once the object is created as an Employee, it can't easily be transformed into a subclass, such as WorkerBee or Engineer, although I suppose one could muck about with setPrototypeOf
.
Instead, you could use a kind of vaguely parasitical approach to inheritance:
function Employee(id) {
this.id = id;
this.WorkerBee = function(project) {
this.project = project;
this.Engineer= function() {
...
return this;
};
return this;
};
}
This is not the inheritance model that your Dad used to program in Java. There is only one constructor, for Employee
. Whether an object is an Engineer
or anything else is purely a matter of what properties or methods it happens to contain, which may be placed there by convenience methods provided such as WorkerBee
. There is no actual WorkerBee
"class", or constructor, or prototype underlying its instances.
This may well bother people coming from languages like Java or C++, but there is nothing really wrong with it, and some people might even say it is more JavaScript-like. There are well-known issues such as the fact that methods reside in the instances, rather than the prototypes. However, if you really want the syntax you proposed, you're going to need to go in this direction or something similar.
Upvotes: 1