construct-newtarget.js (3044B)
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.NumberFormat% works correctly. 9 class MyNumberFormat extends Intl.NumberFormat {} 10 11 var obj = new MyNumberFormat(); 12 assertEq(obj instanceof MyNumberFormat, true); 13 assertEq(obj instanceof Intl.NumberFormat, true); 14 assertEq(Object.getPrototypeOf(obj), MyNumberFormat.prototype); 15 16 obj = Reflect.construct(MyNumberFormat, []); 17 assertEq(obj instanceof MyNumberFormat, true); 18 assertEq(obj instanceof Intl.NumberFormat, true); 19 assertEq(Object.getPrototypeOf(obj), MyNumberFormat.prototype); 20 21 obj = Reflect.construct(MyNumberFormat, [], MyNumberFormat); 22 assertEq(obj instanceof MyNumberFormat, true); 23 assertEq(obj instanceof Intl.NumberFormat, true); 24 assertEq(Object.getPrototypeOf(obj), MyNumberFormat.prototype); 25 26 obj = Reflect.construct(MyNumberFormat, [], Intl.NumberFormat); 27 assertEq(obj instanceof MyNumberFormat, false); 28 assertEq(obj instanceof Intl.NumberFormat, true); 29 assertEq(Object.getPrototypeOf(obj), Intl.NumberFormat.prototype); 30 31 32 // Set a different constructor as NewTarget. 33 obj = Reflect.construct(MyNumberFormat, [], Array); 34 assertEq(obj instanceof MyNumberFormat, false); 35 assertEq(obj instanceof Intl.NumberFormat, false); 36 assertEq(obj instanceof Array, true); 37 assertEq(Object.getPrototypeOf(obj), Array.prototype); 38 39 obj = Reflect.construct(Intl.NumberFormat, [], Array); 40 assertEq(obj instanceof Intl.NumberFormat, false); 41 assertEq(obj instanceof Array, true); 42 assertEq(Object.getPrototypeOf(obj), Array.prototype); 43 44 45 // The prototype defaults to %NumberFormatPrototype% if null. 46 function NewTargetNullPrototype() {} 47 NewTargetNullPrototype.prototype = null; 48 49 obj = Reflect.construct(Intl.NumberFormat, [], NewTargetNullPrototype); 50 assertEq(obj instanceof Intl.NumberFormat, true); 51 assertEq(Object.getPrototypeOf(obj), Intl.NumberFormat.prototype); 52 53 obj = Reflect.construct(MyNumberFormat, [], NewTargetNullPrototype); 54 assertEq(obj instanceof MyNumberFormat, false); 55 assertEq(obj instanceof Intl.NumberFormat, true); 56 assertEq(Object.getPrototypeOf(obj), Intl.NumberFormat.prototype); 57 58 59 // "prototype" property is retrieved exactly once. 60 var trapLog = [], getLog = []; 61 var ProxiedConstructor = new Proxy(Intl.NumberFormat, 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.NumberFormat, [], ProxiedConstructor); 74 assertEqArray(trapLog, ["get"]); 75 assertEqArray(getLog, ["prototype"]); 76 assertEq(obj instanceof Intl.NumberFormat, true); 77 assertEq(Object.getPrototypeOf(obj), Intl.NumberFormat.prototype); 78 79 80 if (typeof reportCompare === "function") 81 reportCompare(0, 0);