• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16declare function print(arg:any):string;
17
18{
19    class A {
20        x?: number;
21        x2?: number;
22        constructor(x: number) {
23            this.x = x;
24        }
25
26        foo() {
27            print("xxxx");
28        }
29    }
30
31    class B extends A {
32        y?: number;
33        constructor(x: number, y: number) {
34            super(x);
35            this.y = y;
36        }
37    }
38
39    // instance define Property
40    let b1 = new B(1, 2);
41    print(b1.hasOwnProperty("x1"));
42    Object.defineProperty(b1, "x1", {value:1});
43    print(b1.hasOwnProperty("x1"));
44
45    // instance delete and change Property
46    let b2 = new B(1, 2);
47    print(b2.hasOwnProperty("y"));
48    print(b2.y);
49    b2.y = 3;
50    print(b2.y);
51    delete b2.y;
52    print(b2.hasOwnProperty("y"));
53
54    // prototype define Property
55    let p = A.prototype;
56    let b3 = new B(1, 2);
57    print(b3.x2);
58    print(Reflect.has(b3, "x2"));
59    Object.defineProperty(p, "x2", {value:1});
60    print(b3.x2);
61    print(Reflect.has(b3, "x2"));
62
63    // prototype delete and change Property
64    let p2 = A.prototype;
65    let b4 = new B(1, 2);
66    print(b4.x);
67    b4.x = 3;
68    print(b4.x);
69    print(b4.hasOwnProperty("x"));
70    delete p2.x;
71    print(b4.hasOwnProperty("x"));
72
73    // prototype change and call function
74    let b5 = new B(1, 2);
75    b5.foo();
76    Object.setPrototypeOf(b5, {})
77    try {
78        b5.foo();
79    } catch(e) {
80        print(e);
81    }
82}
83