1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 22:08:10 +00:00

LibJS: Add object literal getter/setter shorthand

Adds support for the following syntax:

let foo = {
    get x() {
        // ...
    },
    set x(value) {
        // ...
    }
}
This commit is contained in:
Matthew Olsson 2020-05-21 17:28:28 -07:00 committed by Andreas Kling
parent 3a90a01dd4
commit c35732c011
4 changed files with 117 additions and 16 deletions

View file

@ -0,0 +1,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);
}