Inheritance with IIFE

by Artem

JavaScript

'use strict';

let __extends = function(Child, Parent) {
  function __() { // proxy function
    this.constructor = Child;
  }

  __.prototype = Parent.prototype;

  Child.prototype = new __();
  Child.super = Parent.prototype; // specify super class
};

let Person = function(firstName) {};
Person.prototype.getValue = function() {
  return 10;
};

let Employee = (function(_super) {
  __extends(Employee, _super);

  function Employee(firstName, jobName) {
    if (!(this instanceof Employee)) {
      return new Employee(firstName, jobName);
    }
    _super.call(this, firstName);
    this.jobName = jobName;
  }

  Employee.prototype.getJobName = function() {
    return this.jobName;
  }

  Employee.prototype.getValue = function() {
    return _super.prototype.getValue.call(this);
  }

  return Employee;
}(Person));

const e = new Employee();
console.log(e.getValue());