-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththis.test.js
40 lines (31 loc) · 867 Bytes
/
this.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// If you are nesting standard functions notation then this is not lexically bound (will be undefined). To get around this, use Arrow Functions (preferred), .bind, or locally define this outside of the function.
class Test {
constructor() {
this.number = 3;
}
move() {
console.log('works!');
}
test() {
this.move();
function getFirstThis() {
return this;
}
const getSecondThis = () => {
return this;
};
const getThirdThis = getFirstThis.bind(this);
const $this = this;
function getFourthThis() {
return $this;
}
// undefined
console.log(getFirstThis());
// All return "this" context, containing the number property
console.log(this);
console.log(getSecondThis());
console.log(getThirdThis());
console.log(getFourthThis());
}
}
new Test().test();