1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-28 17:15:09 +00:00

LibJS: Implement computed properties in object expressions

This commit is contained in:
Linus Groh 2020-04-23 19:37:53 +01:00 committed by Andreas Kling
parent bebd5c097c
commit 746dd5b190
5 changed files with 98 additions and 19 deletions

View file

@ -1,13 +1,27 @@
load("test-common.js");
try {
var o = { 1: 23, foo: "bar", "hello": "friends" };
var foo = "bar";
var computed = "computed"
var o = {
1: 23,
foo,
bar: "baz",
"hello": "friends",
[1 + 2]: 42,
["I am a " + computed + " key"]: foo,
duplicate: "hello",
duplicate: "world"
};
assert(o[1] === 23);
assert(o["1"] === 23);
assert(o.foo === "bar");
assert(o["foo"] === "bar");
assert(o.hello === "friends");
assert(o["hello"] === "friends");
assert(o[3] === 42);
assert(o["I am a computed key"] === "bar");
assert(o.duplicate === "world");
o.baz = "test";
assert(o.baz === "test");
assert(o["baz"] === "test");
@ -31,6 +45,25 @@ try {
assert(o2.catch === 1);
assert(o2.break === 1);
var a;
var append = x => { a.push(x); };
a = [];
var o3 = {[append(1)]: 1, [append(2)]: 2, [append(3)]: 3}
assert(a.length === 3);
assert(a[0] === 1);
assert(a[1] === 2);
assert(a[2] === 3);
assert(o3.undefined === 3);
a = [];
var o4 = {"test": append(1), "test": append(2), "test": append(3)}
assert(a.length === 3);
assert(a[0] === 1);
assert(a[1] === 2);
assert(a[2] === 3);
assert(o4.test === undefined);
console.log("PASS");
} catch (e) {
console.log("FAIL: " + e);