Feedback on Subclassing" Built-in Constructors

David Bruant bruant.d at gmail.com
Thu Jun 21 08:48:59 PDT 2012


Hi,

I have read the recent "Subclassing" Built-in Constructors" page [1] and 
I have some feedback. This page exhibits a distinction that wasn't clear 
in my mind before reading it: some internal properties can be added 
lazily like the ones for dates while some (essential internal methods) 
cannot.

     class MyDate extends Date {
        constructor(...args) {
           // |this| has no internal date property
           super();
           // now it does have them and can be considered as a date 
object, [[NativeBrand]] aside
        }
     }

For constructor of objects that only add internal data properties and 
additional internal method (like RegExps [[Match]]), this can work fine. 
However, for objects with different essential internal methods, it 
cannot work.

     class MyArray extends Array {
         constructor(...args){
             // |this| not an array until i've called super(), right?
             'length' in this; // false
             this[2] = "yo";
             this.length = 1; // ? I guess nothing happens except that 
|this| has a new 'length' property?
             super(); // If nothing serious happens, the array invariant 
is broken.
             this[2] // ?
             this.length // ?
         }
     }

I don't think there is a way out unless |this| is already an array in 
which case 'super' is useless.

David

[1] http://wiki.ecmascript.org/doku.php?id=strawman:subclassable-builtins


More information about the es-discuss mailing list