| Summary: | ES6 classes: Subclassing arrays doesn't work | ||
|---|---|---|---|
| Product: | WebKit | Reporter: | Nikita Vasilyev <nvasilyev> |
| Component: | JavaScriptCore | Assignee: | Nobody <webkit-unassigned> |
| Status: | RESOLVED DUPLICATE | ||
| Severity: | Normal | CC: | ashvayka, richardconnamacher |
| Priority: | P2 | ||
| Version: | 528+ (Nightly build) | ||
| Hardware: | All | ||
| OS: | All | ||
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() {}
};
This appears to have been fixed in the latest WebKit nightly (as of 1/8/2016) 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 *** |
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