Last active
February 9, 2021 12:12
-
-
Save sksundram/2edcc6ed6b60ba5cd58b91fdd83cd41f to your computer and use it in GitHub Desktop.
JavaScript Context (this) using arrow functions explained by Brad Schiff
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
{"scripts": [],"showConsole": true} |
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
let person = { | |
firstName: 'Steve', | |
lastName: 'Smith', | |
driveCar() { | |
let iAmAFunctionNotAMethod = () => console.log(this); // this === person | |
iAmAFunctionNotAMethod(); | |
console.log(`${this.firstName} ${this.lastName} is driving a car.`); // this === person | |
}, | |
}; | |
person.driveCar(); | |
function breathe() { | |
console.log(`${this.firstName} ${this.lastName} just inhaled and exhaled.`); | |
} | |
breathe.call(person); // this === person |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment