accessors.js (2048B)
1 // |jit-test| skip-if: !getBuildConfiguration("decorators") 2 3 load(libdir + "asserts.js"); 4 5 function assertAccessorDescriptor(object, name) { 6 var desc = Object.getOwnPropertyDescriptor(object, name); 7 assertEq(desc.configurable, true, "The value of `desc.configurable` is `true`"); 8 assertEq(desc.enumerable, false, "The value of `desc.enumerable` is `false`"); 9 assertEq(typeof desc.get, 'function', "`typeof desc.get` is `'function'`"); 10 assertEq(typeof desc.set, 'function', "`typeof desc.set` is `'function'`"); 11 assertEq( 12 'prototype' in desc.get, 13 false, 14 "The result of `'prototype' in desc.get` is `false`" 15 ); 16 assertEq( 17 'prototype' in desc.set, 18 false, 19 "The result of `'prototype' in desc.set` is `false`" 20 ); 21 } 22 23 class C { 24 accessor x; 25 } 26 27 assertAccessorDescriptor(C.prototype, 'x'); 28 29 let c = new C(); 30 assertEq(c.x, undefined, "The value of `c.x` is `undefined` after construction"); 31 c.x = 2; 32 assertEq(c.x, 2, "The value of `c.x` is `2`, after executing `c.x = 2;`"); 33 34 class D { 35 accessor x = 1; 36 } 37 38 assertAccessorDescriptor(D.prototype, 'x'); 39 40 let d = new D(); 41 assertEq(d.x, 1, "The value of `d.x` is `1` after construction"); 42 d.x = 2; 43 assertEq(d.x, 2, "The value of `d.x` is `2`, after executing `d.x = 2;`"); 44 45 class E { 46 accessor #x = 1; 47 48 getX() { 49 return this.#x; 50 } 51 52 setX(v) { 53 this.#x = v; 54 } 55 } 56 57 let e = new E(); 58 assertEq(e.getX(), 1, "The value of `e.x` is `1` after construction"); 59 e.setX(2); 60 assertEq(e.getX(), 2, "The value of `e.x` is `2`, after executing `e.setX(2);`"); 61 62 class F { 63 static accessor x = 1; 64 } 65 66 assertEq(F.x, 1, "The value of `F.x` is `1` after construction"); 67 F.x = 2; 68 assertEq(F.x, 2, "The value of `F.x` is `2`, after executing `F.x = 2;`"); 69 70 assertAccessorDescriptor(F, 'x'); 71 72 class G { 73 static accessor #x = 1; 74 75 getX() { 76 return G.#x; 77 } 78 79 setX(v) { 80 G.#x = v; 81 } 82 } 83 g = new G(); 84 assertEq(g.getX(), 1, "The value of `g.getX()` is `1` after construction"); 85 g.setX(2); 86 assertEq(g.getX(), 2, "The value of `g.getX()` is `2`, after executing `g.setX(2)`");