Skip to content

Instantly share code, notes, and snippets.

@sksundram
Last active February 9, 2021 12:12
Show Gist options
  • Save sksundram/2edcc6ed6b60ba5cd58b91fdd83cd41f to your computer and use it in GitHub Desktop.
Save sksundram/2edcc6ed6b60ba5cd58b91fdd83cd41f to your computer and use it in GitHub Desktop.
JavaScript Context (this) using arrow functions explained by Brad Schiff
{"scripts": [],"showConsole": true}
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