this-access-restriction-2.js (2224B)
1 // Copyright (C) 2014 the V8 project authors. All rights reserved. 2 // This code is governed by the BSD license found in the LICENSE file. 3 /*--- 4 es6id: 14.5 5 description: > 6 class this access restriction 2 7 ---*/ 8 class Base { 9 constructor(a, b) { 10 var o = new Object(); 11 o.prp = a + b; 12 return o; 13 } 14 } 15 16 class Subclass extends Base { 17 constructor(a, b) { 18 var exn; 19 try { 20 this.prp1 = 3; 21 } catch (e) { 22 exn = e; 23 } 24 assert.sameValue( 25 exn instanceof ReferenceError, 26 true, 27 "The result of `exn instanceof ReferenceError` is `true`" 28 ); 29 super(a, b); 30 assert.sameValue(this.prp, a + b, "The value of `this.prp` is `a + b`"); 31 assert.sameValue(this.prp1, undefined, "The value of `this.prp1` is `undefined`"); 32 assert.sameValue( 33 this.hasOwnProperty("prp1"), 34 false, 35 "`this.hasOwnProperty(\"prp1\")` returns `false`" 36 ); 37 return this; 38 } 39 } 40 41 var b = new Base(1, 2); 42 assert.sameValue(b.prp, 3, "The value of `b.prp` is `3`"); 43 44 45 var s = new Subclass(2, -1); 46 assert.sameValue(s.prp, 1, "The value of `s.prp` is `1`"); 47 assert.sameValue(s.prp1, undefined, "The value of `s.prp1` is `undefined`"); 48 assert.sameValue( 49 s.hasOwnProperty("prp1"), 50 false, 51 "`s.hasOwnProperty(\"prp1\")` returns `false`" 52 ); 53 54 class Subclass2 extends Base { 55 constructor(x) { 56 super(1,2); 57 58 if (x < 0) return; 59 60 var called = false; 61 function tmp() { called = true; return 3; } 62 var exn = null; 63 try { 64 super(tmp(),4); 65 } catch (e) { exn = e; } 66 assert.sameValue( 67 exn instanceof ReferenceError, 68 true, 69 "The result of `exn instanceof ReferenceError` is `true`" 70 ); 71 assert.sameValue(called, true, "The value of `called` is `true`"); 72 } 73 } 74 75 var s2 = new Subclass2(1); 76 assert.sameValue(s2.prp, 3, "The value of `s2.prp` is `3`"); 77 78 var s3 = new Subclass2(-1); 79 assert.sameValue(s3.prp, 3, "The value of `s3.prp` is `3`"); 80 81 assert.throws(TypeError, function() { Subclass.call(new Object(), 1, 2); }); 82 assert.throws(TypeError, function() { Base.call(new Object(), 1, 2); }); 83 84 class BadSubclass extends Base { 85 constructor() {} 86 } 87 88 assert.throws(ReferenceError, function() { new BadSubclass(); }); 89 90 reportCompare(0, 0);