1#  Reassigning object methods is not supported
2
3Rule ``arkts-no-method-reassignment``
4
5**Severity: error**
6
7ArkTS does not support re-assigning a method for objects. In the statically
8types languages, the layout of objects is fixed and all instances of the same
9object must share the same code of each method.
10
11If you need to add specific behavior for certain objects, you can create
12separate wrapper functions or use inheritance.
13
14
15## TypeScript
16
17
18```
19
20    class C {
21        foo() {
22            console.log("foo")
23        }
24    }
25
26    function bar() {
27        console.log("bar")
28    }
29
30    let c1 = new C()
31    let c2 = new C()
32    c2.foo = bar
33
34    c1.foo() // foo
35    c2.foo() // bar
36
37```
38
39## ArkTS
40
41
42```
43
44    class C {
45        foo() {
46            console.log("foo")
47        }
48    }
49
50    class Derived extends C {
51        foo() {
52            console.log("Extra")
53            super.foo()
54        }
55    }
56
57    function bar() {
58        console.log("bar")
59    }
60
61    let c1 = new C()
62    let c2 = new C()
63    c1.foo() // foo
64    c2.foo() // foo
65
66    let c3 = new Derived()
67    c3.foo() // Extra foo
68
69```
70
71
72