Created
July 24, 2020 10:10
-
-
Save fiWhy/09302158aa84bdc73d4b6c93da5df236 to your computer and use it in GitHub Desktop.
Function Constructor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const grades = { | |
Junior: 'junior', | |
Middle: 'middle', | |
Senior: 'senior', | |
}; | |
const bonuses = { | |
'C++': 100, | |
Rust: 150, | |
default: 50, | |
}; | |
const gradeTax = { | |
[grades.Junior]: 0.25, | |
[grades.Middle]: 0.5, | |
[grades.Senior]: 0.75, | |
}; | |
function User(name, language, grade = grades.Junior) { | |
this.name = name; | |
this.grade = grade; | |
this.salary = 1000; | |
this.language = language; | |
this.tasks = 0; | |
this.addTask = () => { | |
this.tasks++; | |
}; | |
/** | |
* This method... | |
*/ | |
this.finishTask = () => { | |
if (this.tasks > 0) { | |
this.tasks--; | |
this.salary += | |
(bonuses[this.language] || bonuses.default) * gradeTax[this.grade]; | |
} | |
}; | |
} | |
const user = new User('John', 'C++', grades.Junior); | |
const user1 = new User('Vasya', 'Rust', grades.Senior); | |
const user2 = new User('Nifertiti', 'Bu', grades.Middle); | |
user.addTask(); | |
user.addTask(); | |
user.addTask(); | |
user.addTask(); | |
user.addTask(); | |
user.finishTask(); | |
user.finishTask(); | |
user.finishTask(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment