I am trying to extend the Array in native JS. But not just modifying the prototype. I thought I'd give it a shot with the new class
es2015 syntax.
Trying it out native (chrome already supports it - yay ヽ(´▽`)ノ) and it all works just fine. Switching over to Babel for better support, it stops working. After trying a bit, I saw, that none of the methods are available on the new Object. Test code:
class CustomList extends Array {
constructor(...elms) {
super(...elms);
this.testprop = "test";
}
customMethod(a) {
console.log(a, this);
}
}
答案 0 :(得分:3)
Babel.js can't extend the built-in classes properly:
Built-in classes such as
Date
,Array
,DOM
etc cannot be properly subclassed due to limitations in ES5 (for thees2015-classes
plugin). You can try to usebabel-plugin-transform-builtin-extend
based onObject.setPrototypeOf
andReflect.construct
, but it also has some limitations.