-
Book Overview & Buying
-
Table Of Contents
Full-Stack React, TypeScript, and Node
By :
We already discussed the this context object in the previous section. As mentioned, in JavaScript, functions have access to an internal object called this that represents the caller of the function. Now, the confusing part of using this is that the value of this can change depending on how the function is called. So, JavaScript provides helpers that allow you to reset the this object of a function to the one you want, instead of the one given to you. There are several methods, including apply and call, but the most important one for us to learn is the bind keyword. This is important for us to know because bind is used often in React class-based components. It's a bit early to show a full-blown React example. So, let's start with something a little easier. Create a new file called bind.ts and add the following code to it:
class A {
name: string = 'A';
go() {
...