-
Notifications
You must be signed in to change notification settings - Fork 1
/
closure_this.ts
36 lines (30 loc) · 967 Bytes
/
closure_this.ts
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
class Test {
arr: Array<string> = ['a', 'b', 'c', 'd']
loopArrow() {
let _this = this
let a = 'aaa'
this.arr.forEach(item => {
console.log('~~~~~~item', item)
// [ERROR] console - Program terminated with exit(1)
console.log('~~~~~~~arr.len ' , this.arr.length)
})
}
loopSimple() {
let _this = this
let a = 'bbb'
this.arr.forEach(function(item) {
console.log('~~~~~~item', item)
// 符合预期
// error TS2683: 'this' implicitly has type 'any' because it does not have a type annotation.
// 'this' implicitly has type 'any' because it does not have a type annotation.
// console.log('~~~~~~~arr.len ' , this.arr.length)
// work
console.log('~~~~~~~arr.len ' , _this.arr.length)
// work
console.log('~~~~~~~~~~a', a)
})
}
}
let t: Test = new Test()
t.loopArrow()
t.loopSimple()