Skip to content

Instantly share code, notes, and snippets.

@kenanhancer
Created April 8, 2018 15:46
Show Gist options
  • Save kenanhancer/f28f3de0036e8a648466ac4b6a87d100 to your computer and use it in GitHub Desktop.
Save kenanhancer/f28f3de0036e8a648466ac4b6a87d100 to your computer and use it in GitHub Desktop.
FunctionInheritanceTutorial1 created by kenanhancer - https://repl.it/@kenanhancer/FunctionInheritanceTutorial1
let Person = function(personId, firstName, lastName, age, gender) {
this.personId = personId;
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
this.gender = gender;
};
Person.prototype.getFullName = function() {
return `${this.firstName} ${this.lastName}`;
};
Person.prototype.greet = function() {
return `Hello ${this.getFullName()}`;
};
let Employee = function() {
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);
};
//INHERITANCE
Employee.prototype = Object.create(Person.prototype);
Employee.prototype.constructor = Employee;
Employee.prototype.calculateGrossSalary = function() {
let netSalary = this.calculateNetSalary();
return netSalary * (1 + this.taxRate);
};
Employee.prototype.calculateNetSalary = function() {
return this.salary * 12;
};
let Manager = function() {
let args = Array.prototype.slice.call(arguments);
Employee.apply(this, args);
this.taxRate = 0.21;
};
//INHERITANCE
Manager.prototype = Object.create(Employee.prototype);
Manager.prototype.constructor = Manager;
//OVERRIDE
Manager.prototype.greet = function() {
let _greet = Employee.prototype.greet.call(this);
return `${_greet}, Welcome!`;
};
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