dimitrix85

02.People

Nov 10th, 2019
73
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function solveClass() {
  2.     class Employee {
  3.         constructor(name, age) {
  4.             if (new.target === Employee) {
  5.                 throw new Error("Canon instantiate directly.");
  6.             }
  7.             this.name = name;
  8.             this.age = age;
  9.             this.salary = 0;
  10.             this.tasks = []
  11.         }
  12.  
  13.         work() {
  14.             let currentTask = this.tasks.shift();
  15.             console.log(this.name + currentTask);
  16.             this.tasks.push(currentTask);
  17.         }
  18.  
  19.         collectSalary() {
  20.             console.log(`${this.name} received ${this.getSalary()} this month.`);
  21.         }
  22.         getSalary() {
  23.             return this.salary;
  24.         }
  25.     }
  26.  
  27.     class Junior extends Employee {
  28.         constructor(name, age) {
  29.             super(name, age);
  30.             this.tasks.push(" is working on a simple task.");
  31.         }
  32.     }
  33.  
  34.     class Senior extends Employee {
  35.         constructor(name, age) {
  36.             super(name, age);
  37.             this.tasks.push(" is working on a complicated task.");
  38.             this.tasks.push(" is taking time off work.");
  39.             this.tasks.push(" is supervising junior workers.");
  40.         }
  41.     }
  42.  
  43.     class Manager extends Employee {
  44.         constructor(name, age) {
  45.             super(name, age);
  46.             this.dividend = 0;
  47.             this.tasks.push(" scheduled a meeting.");
  48.             this.tasks.push(" is preparing a quarterly report.");
  49.         }
  50.  
  51.         getSalary() {
  52.             return this.salary + this.dividend;
  53.         }
  54.     }
  55.  
  56.     return {
  57.         Employee,
  58.         Junior,
  59.         Senior,
  60.         Manager
  61.     }
  62. }
  63.  
  64. function solveProto() {
  65.  
  66.     function Employee(name, age) {
  67.         if (new.target === Employee) {
  68.             throw new Error("Canon instantiate directly.");
  69.         }
  70.         this.name = name;
  71.         this.dividend = 0;
  72.         this.age = age;
  73.         this.salary = 0;
  74.         this.tasks = [];
  75.     }
  76.  
  77.     Employee.prototype.work = function () {
  78.         let currentTask = this.tasks.shift();
  79.         console.log(this.name + currentTask);
  80.         this.tasks.push(currentTask);
  81.     };
  82.  
  83.     Employee.prototype.collectSalary = function () {
  84.         console.log(`${this.name} received ${this.getSalary() + this.dividend} this month.`);
  85.     };
  86.  
  87.     Employee.prototype.getSalary = function () {
  88.         return this.salary;
  89.     };
  90.  
  91.     function Junior(name, age) {
  92.         Employee.call(this, name, age);
  93.         this.tasks.push(" is working on a simple task.");
  94.     }
  95.  
  96.     // Object.setPrototypeOf(Junior, Employee);
  97.     // Junior.prototype.constructor = Junior;
  98.     Junior.prototype = Object.create(Employee.prototype);
  99.  
  100.     function Senior(name, age) {
  101.         Employee.call(this, name, age);
  102.         this.tasks.push(" is working on a complicated task.");
  103.         this.tasks.push(" is taking time off work.");
  104.         this.tasks.push(" is supervising junior workers.");
  105.     }
  106.  
  107.     // Object.setPrototypeOf(Senior, Employee);
  108.     // Senior.prototype.constructor = Senior;
  109.     Senior.prototype = Object.create(Employee.prototype);
  110.  
  111.     function Manager(name, age) {
  112.         Employee.call(this, name, age);
  113.         this.tasks.push(" scheduled a meeting.");
  114.         this.tasks.push(" is preparing a quarterly report.");
  115.     }
  116.  
  117.     // Object.setPrototypeOf(Manager, Employee);
  118.     // Manager.prototype.constructor = Manager;
  119.     Manager.prototype = Object.create(Employee.prototype);
  120.  
  121.     return {
  122.         Employee,
  123.         Junior,
  124.         Senior,
  125.         Manager
  126.     }
  127. }
RAW Paste Data