Table of contents
The first type: when calling a function through an instance:
The second type: when calling a function by deconstructing an instance:
1. When declaring a function, declare it in the form of a normal function declaration:
2. When the function is declared, use the form of the arrow function
The first type: when calling a function through an instance:
class My {
add(a) {
console.log(this)
}
}
const my = new My()
my.add(3)
When the function is called, this is printed, and the result is the instance itself:
The second type: when calling a function by deconstructing an instance:
1. When declaring a function, declare it in the form of a normal function declaration:
class My {
add(a) {
console.log(this)
}
}
const {add} = new My()
add(2)
Destructure the instance object My into attribute add, when calling the destructured value, print out this, the result is: undefined
2. When the function is declared, use the form of the arrow function
class My {
add = (a) => {
console.log(this)
}
}
const {add} = new My()
add(2)
The result of the operation is that this is an instance of the object My: