summaryrefslogtreecommitdiff
path: root/Libraries/LibJS/Tests/object-getter-setter-shorthand.js
blob: e0ca209fa565073d27a44929ca0bbcdbaedac24d (plain)
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
41
42
43
44
45
46
47
48
49
50
load("test-common.js")

try {
    let o = {
        get() { return 5; },
        set() { return 10; },
    };
    assert(o.get() === 5);
    assert(o.set() === 10);

    o = {
        get x() { return 5; },
        set x(_) { },
    };
    assert(o.x === 5);
    o.x = 10;
    assert(o.x === 5);

    o = {
        get x() {
            return this._x + 1;
        },
        set x(value) {
            this._x = value + 1;
        },
    };

    assert(isNaN(o.x));
    o.x = 10;
    assert(o.x === 12);
    o.x = 20;
    assert(o.x === 22);

    o = {
        get x() { return 5; },
        get x() { return 10; },
    };

    assert(o.x === 10);

    o = {
        set x(value) { return 10; },
    };

    assert((o.x = 20) === 20);

    console.log("PASS");
} catch (e) {
    console.log("FAIL: " + e);
}