Last active
May 27, 2022 08:27
-
-
Save crutchcorn/a32eb1fe6d0b7b15b4f4045160445181 to your computer and use it in GitHub Desktop.
Explaination of `this` keyword in JavaScript through a series of Tweets
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
class Cup { | |
contents = "water"; | |
consume() { | |
console.log( | |
"You drink the ", | |
this.contents, | |
". Hydrating!" | |
); | |
} | |
} | |
const cup = new Cup(); | |
// This will log "You drink the water. Hydrating!" | |
cup.consume(); |
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
class Cup { | |
contents = "water"; | |
consume = () => { | |
console.log( | |
"You drink the ", this.contents, ". Hydrating!" | |
); | |
} | |
} | |
class Bowl { | |
contents = "chili"; | |
consume = () => { | |
console.log("You eat the ", this.contents, ". Spicy!"); | |
} | |
} | |
cup = new Cup(); | |
bowl = new Bowl(); | |
cup.consume = bowl.consume; | |
// This will log "You eat the chili. Spicy!" | |
// Because `this` from an arrow function is not rebound | |
cup.consume(); | |
// Even if we try to explictly bind to "cup" | |
// it won't work. Arrow functions can't be re-bound | |
cup.consume = bowl.consume.bind(cup); |
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
@Component({ | |
selector: 'paragraph', | |
template: ` | |
<button #btn>Add one</button> | |
<p>Count is {{count}}</p> | |
` | |
}) | |
class RenderParagraphComponent implements AfterViewInit, OnDestroy { | |
@ViewChild('btn') btn: ElementRef<HTMLElement>; | |
count = 0; | |
// No need to `bind` anymore! | |
// `this` will ALWAYS reference the component | |
addOne = () => { | |
this.count++; | |
} | |
ngAfterViewInit() { | |
this.btn.nativeElement | |
.addEventListener('click', this.addOne); | |
} | |
ngOnDestroy() { | |
this.btn.nativeElement | |
.removeEventListener('click', this.addOne); | |
} | |
} |
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
class Cup { | |
contents = "water"; | |
consume() { | |
console.log( | |
"You drink the ", this.contents, ". Hydrating!" | |
); | |
} | |
} | |
class Bowl { | |
contents = "chili"; | |
consume() { | |
console.log( | |
"You eat the ", this.contents, ". Spicy!" | |
); | |
} | |
} | |
cup = new Cup(); | |
bowl = new Bowl(); | |
// This will log "You drank the water. Hydrating!"" | |
cup.consume(); | |
// This will log "You eat the chili. Spicy!" | |
bowl.consume(); |
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
class Cup { | |
contents = "water"; | |
consume() { | |
console.log( | |
"You drink the ", this.contents, ". Hydrating!" | |
); | |
} | |
} | |
class Bowl { | |
contents = "chili"; | |
consume() { | |
console.log( | |
"You eat the ", this.contents, ". Spicy!" | |
); | |
} | |
} | |
cup = new Cup(); | |
bowl = new Bowl(); | |
// This is assigning the `bowl.consume` message | |
cup.consume = bowl.consume; | |
// But using the `cup.contents` `this` scoping | |
cup.consume(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment