Advertisement
Todorov_Stanimir

02. People Exercise: Prototypes and Inheritance

Nov 9th, 2019
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function () {
  2.     function Employee(name, age) {
  3.         this.name = name;
  4.         this.age = age;
  5.         this.salary = 0;
  6.         this.dividend = 0;
  7.         this.tasks = [];
  8.  
  9.         this.work = function () {
  10.             let currentTask = this.tasks.shift();
  11.             console.log(`${this.name}${currentTask}`);
  12.             this.tasks.push(currentTask);
  13.         }
  14.         this.collectSalary = function () {
  15.             console.log(`${this.name} received ${this.salary + this.dividend} this month.`);
  16.         };
  17.     }
  18.  
  19.     function Junior(name, age) {
  20.         Employee.call(this, name, age);
  21.         this.tasks = [' is working on a simple task.'];
  22.     }
  23.  
  24.     Junior.prototype = Object.create(Employee.prototype);
  25.     // Object.setPrototypeOf(Junior, Employee);
  26.     // Junior.prototype.constructor = Junior;
  27.  
  28.     function Senior(name, age) {
  29.  
  30.         Employee.call(this, name, age);
  31.         this.tasks = [' is working on a complicated task.', ' is taking time off work.', ' is supervising junior workers.'];
  32.     }
  33.  
  34.     Senior.prototype = Object.create(Employee.prototype);
  35.     // Object.setPrototypeOf(Senior, Employee)
  36.     // Senior.prototype.constructor = Senior;
  37.  
  38.     function Manager(name, age) {
  39.         Employee.call(this, name, age);
  40.         this.tasks = [' scheduled a meeting.', ' is preparing a quarterly report.'];
  41.     }
  42.  
  43.     Manager.prototype = Object.create(Employee.prototype);
  44.     // Object.setPrototypeOf(Manager, Employee);
  45.     // Manager.prototype.constructor = Manager;
  46.  
  47.     return { Employee, Junior, Senior, Manager }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement