Bug 145155

Summary: ES6 classes: Subclassing arrays doesn't work
Product: WebKit Reporter: Nikita Vasilyev <nvasilyev>
Component: JavaScriptCoreAssignee: Nobody <webkit-unassigned>
Status: RESOLVED DUPLICATE    
Severity: Normal CC: ashvayka, richardconnamacher
Priority: P2    
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   

Description Nikita Vasilyev 2015-05-18 17:48:56 PDT
var MyArray = class MyArray extends Array {
    constructor(len) {
        super(len);
    }

    myMethod() {}
};


var myArr = new MyArray(0);

console.log(myArr.myMethod); // 'function' in Chrome 42+, undefined in WebKit ToT
console.log(myArr instanceof MyArray); // true in Chrome 42+, false in WebKit ToT

myArr.push(1);
console.log(myArr instanceof MyArray); // true in Chrome 42+, false in WebKit ToT
Comment 1 Richard Connamacher 2015-12-02 11:13:57 PST
WebKit isn't setting the right prototype chain when instantiating an Array subclass. Because of this, the following workaround restores the correct behavior in the WebKit nightly:

var MyArray = class MyArray extends Array {
    constructor() {
        super(...arguments);
        Object.setPrototypeOf(this, new.target.prototype);
    }

    myMethod() {}
};
Comment 2 Richard Connamacher 2016-01-08 13:39:57 PST
This appears to have been fixed in the latest WebKit nightly (as of 1/8/2016)
Comment 3 Alexey Shvayka 2020-03-10 15:31:31 PDT
Fixed in https://trac.webkit.org/changeset/194612.

Tests:
  JSTests/ChakraCore/test/es6/classes.js
  JSTests/ChakraCore/test/es6/ES6SubclassableBuiltins.js
  JSTests/es6/Array_is_subclassable_*.js
  JSTests/test262/test/language/statements/class/subclass/builtin-objects/Array/*.js

*** This bug has been marked as a duplicate of bug 152706 ***