Last active
February 10, 2018 17:24
-
-
Save OneCent01/f616f4015e361fabb34309622cf27a57 to your computer and use it in GitHub Desktop.
Employee Objects - modified code originally found at: http://www.dofactory.com/javascript/factory-method-design-pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var subclasses = { | |
Employee: function(name, role) { | |
this.name = name; | |
this.sayHi = function() { | |
console.log(this.name + ": I work " + this.weeklyHours + "hrs/week"); | |
}; | |
if(role === "manager" || role === "salesman") return subclasses.Fulltime(name, role); | |
else if(role === "temporary" || role === "traveling") return subclasses.Parttime(name, role); | |
}, | |
Fulltime: function(name, role) { | |
subclasses.Employee.call(this, name); | |
this.weeklyHours = 40; | |
if(role === "manager") return new subclasses.Manager(name); | |
else if(role === "salesman") return new subclasses.Salesman(name); | |
}, | |
Parttime: function(name, role) { | |
subclasses.Employee.call(this, name); | |
this.weeklyHours = 20; | |
if(role === "temporary") return new subclasses.Temporary(name); | |
}, | |
Manager: function(name) { | |
subclasses.Fulltime.call(this, name); | |
this.hourly = 25; | |
}, | |
Salesman: function(name) { | |
subclasses.Fulltime.call(this, name); | |
this.hourly = 20; | |
}, | |
Temporary: function(name) { | |
subclasses.Parttime.call(this, name); | |
this.hourly = 14; | |
} | |
}; | |
var employeeFactoryMethod = function(name, role) { | |
var Employee = subclasses.Employee; | |
return Employee(name, role); | |
}; | |
var generateOfficeCast = function() { | |
var cast = []; | |
cast.push(employeeFactoryMethod('Michael', 'manager')); | |
cast.push(employeeFactoryMethod('Jim', 'salesman')); | |
cast.push(employeeFactoryMethod('Ryan', 'temporary')); | |
return cast; | |
}; | |
var officeCast = generateOfficeCast(); | |
officeCast.forEach(actor => actor.sayHi()); | |
officeCast[0]; // michael object | |
// from (http://www.dofactory.com/javascript/factory-method-design-pattern) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment