Adding a non-class-syntax method for subclassing native objects

Allen Wirfs-Brock allen at wirfs-brock.com
Wed May 4 17:57:46 UTC 2016


> On May 3, 2016, at 8:01 PM, Michael Theriot <michael.lee.theriot at gmail.com> wrote:
> 
> I believe you can do this with `Reflect.construct`.
> 
> ```js
> function SubArray(arg1) {
>   return Reflect.construct(Array, arguments, SubArray);

this should probably be: return Reflect.construct(Array, arguments, new.target);

If you don’t pass new.target subclasses of SubArray  (whether defined using a class declaration or procedurally) won’t be correctly constructed




> }
> SubArray.prototype = Object.create(Array.prototype);
> Reflect.setPrototypeOf(SubArray, Array);
> 
> var arr = new SubArray();
> arr instanceof SubArray; // true
> arr instanceof Array; // true
> arr[5] = 0; // should exotically update length property
> arr.length === 6; // true
> ```
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss



More information about the es-discuss mailing list