• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#  Using ``this`` inside stand-alone functions is not supported
2
3Rule ``arkts-no-standalone-this``
4
5**Severity: error**
6
7ArkTS does not support the usage of ``this`` inside stand-alone functions and
8inside static methods. ``this`` can be used in instance methods only.
9
10
11## TypeScript
12
13
14```
15
16    function foo(i: number) {
17        this.count = i // Compile-time error only with noImplicitThis
18    }
19
20    class A {
21        count: number = 1
22        m = foo
23    }
24
25    let a = new A()
26    console.log(a.count) // prints "1"
27    a.m(2)
28    console.log(a.count) // prints "2"
29
30
31```
32
33## ArkTS
34
35
36```
37
38    class A {
39        count: number = 1
40        m(i: number): void {
41            this.count = i
42        }
43    }
44
45    function main(): void {
46        let a = new A()
47        console.log(a.count)  // prints "1"
48        a.m(2)
49        console.log(a.count)  // prints "2"
50    }
51
52```
53
54## See also
55
56- Recipe 140:  ``Function.apply``, ``Function.bind``, ``Function.call`` are not supported (``arkts-no-func-apply-bind-call``)
57
58
59