private-throwing-initializer.js (892B)
1 // Ensure private fields are stamped in order and that 2 // we can successfully partially initialize objects. 3 4 class Base { 5 constructor(o) { 6 return o; 7 } 8 } 9 10 let constructorThrow = false; 11 12 function maybeThrow() { 13 if (constructorThrow) { 14 throw 'fail' 15 } 16 return 'sometimes' 17 } 18 19 class A extends Base { 20 constructor(o) { 21 super(o); 22 constructorThrow = !constructorThrow; 23 } 24 25 #x = 'always'; 26 #y = maybeThrow(); 27 28 static gx(o) { 29 return o.#x; 30 } 31 32 static gy(o) { 33 return o.#y; 34 } 35 }; 36 37 var obj1 = {}; 38 var obj2 = {}; 39 40 new A(obj1); 41 42 var threw = true; 43 try { 44 new A(obj2); 45 threw = false; 46 } catch (e) { 47 assertEq(e, 'fail'); 48 } 49 assertEq(threw, true); 50 51 A.gx(obj1) 52 A.gx(obj2); // Both objects get x; 53 A.gy(obj1); // obj1 gets y 54 55 try { 56 A.gy(obj2); // shouldn't have x. 57 threw = false; 58 } catch (e) { 59 assertEq(e instanceof TypeError, true); 60 } 61 62 assertEq(threw, true);