Bug 145155 - ES6 classes: Subclassing arrays doesn't work
Summary: ES6 classes: Subclassing arrays doesn't work
Status: RESOLVED DUPLICATE of bug 152706
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Nobody
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2015-05-18 17:48 PDT by Nikita Vasilyev
Modified: 2020-03-10 15:31 PDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 ***