Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Created April 8, 2018 15:52
Show Gist options
  • Save kenanhancer/c1fd34bf5fc3dc218812d8a47930056b to your computer and use it in GitHub Desktop.
Save kenanhancer/c1fd34bf5fc3dc218812d8a47930056b to your computer and use it in GitHub Desktop.
FunctionInheritanceTutorial2 created by kenanhancer - https://repl.it/@kenanhancer/FunctionInheritanceTutorial2
Function.prototype.inherits = function(parentClassOrObject) {
let child = this;
function temp() {
this.constructor = child;
}
temp.prototype = parentClassOrObject.prototype;
this.prototype = new temp();
return this;
};
let Person = (function() {
function Person(personId, firstName, lastName, age, gender) {
this.personId = personId;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
this.created = Date.now();
this.updated = Date.now();
this.isDeleted = false;
}
Person.prototype.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};
Person.prototype.greet = function() {
return `Hello ${this.getFullName()}`;
};
return Person;
})();
let Employee = (function() {
Employee.inherits(Person);
function Employee() {
let args = Array.prototype.slice.call(arguments);
let _title = args[5];
let _salary = args[6];
if (_title) {
this.title = _title;
}
if (_salary) {
this.salary = _salary;
}
this.taxRate = 0.14;
Person.apply(this, args);
}
Employee.prototype.calculateGrossSalary = function() {
let netSalary = this.calculateNetSalary();
return netSalary * (1 + this.taxRate);
};
Employee.prototype.calculateNetSalary = function() {
return this.salary * 12;
};
return Employee;
})();
let Manager = (function() {
Manager.inherits(Employee);
function Manager() {
let args = Array.prototype.slice.call(arguments);
Employee.apply(this, args);
this.taxRate = 0.21;
}
Manager.prototype.greet = function() {
let _greet = Employee.prototype.greet.call(this);
return `${_greet}, Welcome!`;
};
return Manager;
})();
let employee1 = new Employee(1, 'Kenan', 'Hancer', 33, 'MALE', 'WORKER', 1000);
let manager1 = new Manager(2, 'Enes', 'Demir', 40, 'MALE', 'MANAGER', 5000);
console.log(employee1);
console.log(employee1.getFullName());
console.log(employee1.greet());
console.log(employee1.calculateNetSalary());
console.log(employee1.calculateGrossSalary());
console.log();
console.log(manager1);
console.log(manager1.getFullName());
console.log(manager1.greet());
console.log(manager1.calculateNetSalary());
console.log(manager1.calculateGrossSalary());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment