在ES6与Babel扩展内置的本地人 [英] Extending built-in natives in ES6 with Babel
问题描述
我正在使用Babel扩展我的课程与内置本机Array
I'm using Babel for extend my class with the built-in native Array
class NewArray extends Array {
first() {
return this[0];
}
}
var a = new NewArray(1, 2, 3);
console.log(a.length); // 3
console.log(a); // [ 1, 2, 3 ]
console.log(a.first()); // trigger error
在a.first()中,我收到此错误:
In a.first(), I'm getting this error:
console.log(a.first());
^
TypeError: a.first is not a function
我应该做更多的事情来扩展一个内置的native?
Should I do something more to extend a built-in native?
谢谢!
推荐答案
Babel不支持扩展本地类。
它已在5.2.17版中删除(请参阅此提交)
Extending native classes is not supported by Babel. It was removed in version 5.2.17 (see this commit)
它被删除,因为它不能正常工作,请查看错误: https://phabricator.babeljs.io/T1424
It was removed because it was not working properly, see the bug: https://phabricator.babeljs.io/T1424
这不太可能会被添加,因为它不是可以模拟的功能。我们必须在浏览器中等待本地支持(有些已经在实验模式下支持了)。这也意味着它将在不同的浏览器中的行为不同。
It's unlikely it will be ever added because it's not a feature that can be simulated. We will have to wait for native support in browsers (some already support it now in experimental mode). That also means it will currently behave differently in different browsers.
这篇关于在ES6与Babel扩展内置的本地人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!