Last active
July 30, 2021 01:34
-
-
Save sergiopichardo/3cebff51c894c9ee0a50c794da088faa to your computer and use it in GitHub Desktop.
Preserve the execution context using a variable in the outer scope
This file contains hidden or 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
| class Employee { | |
| constructor(name, lastName, salary) { | |
| this.name = name; | |
| this.lastName = lastName; | |
| this.salary = salary; | |
| } | |
| getSalaryInfo() { | |
| let self = this; // use a variable in outer scope to | |
| // preserver the value of `this` | |
| function calculateMonthlyWages() { | |
| return { | |
| name: self.name + ' ' + self.lastName, | |
| monthlyWages: self.salary / 12, | |
| salary: self.salary | |
| } | |
| } | |
| return calculateMonthlyWages(); | |
| } | |
| } | |
| const alexis = new Employee('Alexis', 'Rose', 500_000) | |
| console.log(alexis.getSalaryInfo()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment