mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 11:58:12 +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:
parent
3a90a01dd4
commit
c35732c011
4 changed files with 117 additions and 16 deletions
50
Libraries/LibJS/Tests/object-getter-setter-shorthand.js
Normal file
50
Libraries/LibJS/Tests/object-getter-setter-shorthand.js
Normal 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);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue