construct-newtarget.js (3120B)
1 // |reftest| skip-if(!this.hasOwnProperty("Intl")) 2 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 8 // Test subclassing %Intl.DateTimeFormat% works correctly. 9 class MyDateTimeFormat extends Intl.DateTimeFormat {} 10 11 var obj = new MyDateTimeFormat(); 12 assertEq(obj instanceof MyDateTimeFormat, true); 13 assertEq(obj instanceof Intl.DateTimeFormat, true); 14 assertEq(Object.getPrototypeOf(obj), MyDateTimeFormat.prototype); 15 16 obj = Reflect.construct(MyDateTimeFormat, []); 17 assertEq(obj instanceof MyDateTimeFormat, true); 18 assertEq(obj instanceof Intl.DateTimeFormat, true); 19 assertEq(Object.getPrototypeOf(obj), MyDateTimeFormat.prototype); 20 21 obj = Reflect.construct(MyDateTimeFormat, [], MyDateTimeFormat); 22 assertEq(obj instanceof MyDateTimeFormat, true); 23 assertEq(obj instanceof Intl.DateTimeFormat, true); 24 assertEq(Object.getPrototypeOf(obj), MyDateTimeFormat.prototype); 25 26 obj = Reflect.construct(MyDateTimeFormat, [], Intl.DateTimeFormat); 27 assertEq(obj instanceof MyDateTimeFormat, false); 28 assertEq(obj instanceof Intl.DateTimeFormat, true); 29 assertEq(Object.getPrototypeOf(obj), Intl.DateTimeFormat.prototype); 30 31 32 // Set a different constructor as NewTarget. 33 obj = Reflect.construct(MyDateTimeFormat, [], Array); 34 assertEq(obj instanceof MyDateTimeFormat, false); 35 assertEq(obj instanceof Intl.DateTimeFormat, false); 36 assertEq(obj instanceof Array, true); 37 assertEq(Object.getPrototypeOf(obj), Array.prototype); 38 39 obj = Reflect.construct(Intl.DateTimeFormat, [], Array); 40 assertEq(obj instanceof Intl.DateTimeFormat, false); 41 assertEq(obj instanceof Array, true); 42 assertEq(Object.getPrototypeOf(obj), Array.prototype); 43 44 45 // The prototype defaults to %DateTimeFormatPrototype% if null. 46 function NewTargetNullPrototype() {} 47 NewTargetNullPrototype.prototype = null; 48 49 obj = Reflect.construct(Intl.DateTimeFormat, [], NewTargetNullPrototype); 50 assertEq(obj instanceof Intl.DateTimeFormat, true); 51 assertEq(Object.getPrototypeOf(obj), Intl.DateTimeFormat.prototype); 52 53 obj = Reflect.construct(MyDateTimeFormat, [], NewTargetNullPrototype); 54 assertEq(obj instanceof MyDateTimeFormat, false); 55 assertEq(obj instanceof Intl.DateTimeFormat, true); 56 assertEq(Object.getPrototypeOf(obj), Intl.DateTimeFormat.prototype); 57 58 59 // "prototype" property is retrieved exactly once. 60 var trapLog = [], getLog = []; 61 var ProxiedConstructor = new Proxy(Intl.DateTimeFormat, new Proxy({ 62 get(target, propertyKey, receiver) { 63 getLog.push(propertyKey); 64 return Reflect.get(target, propertyKey, receiver); 65 } 66 }, { 67 get(target, propertyKey, receiver) { 68 trapLog.push(propertyKey); 69 return Reflect.get(target, propertyKey, receiver); 70 } 71 })); 72 73 obj = Reflect.construct(Intl.DateTimeFormat, [], ProxiedConstructor); 74 assertEqArray(trapLog, ["get"]); 75 assertEqArray(getLog, ["prototype"]); 76 assertEq(obj instanceof Intl.DateTimeFormat, true); 77 assertEq(Object.getPrototypeOf(obj), Intl.DateTimeFormat.prototype); 78 79 80 if (typeof reportCompare === "function") 81 reportCompare(0, 0);