创建一个从ES6 Map扩展的类 [英] Create a class extending from ES6 Map
问题描述
Chrome版本41.0.2272.101 m
class MyMap extends Map {
get(key){
if(!this.has(key)){throw new Error(...); }
return super.get(key);
}
set(key){
if(this.has(key)){throw new Error(...); }
return super.set(key);
}
}
不知道如果我的语法错误或我我错过了某种实现。但是我收到以下错误:
方法Map.prototype.forEach调用不兼容的收件人
Babel明确表示,他们不支持扩展内置的类。请参阅 http://babeljs.io/docs/usage/caveats/#classes 。原因并不如ES5中的限制那么简单,因为 Map
不是开始的ES5功能。似乎Map的实现不支持基本模式,例如
Map.prototype.set.call(mymap,'key' ,1);
这在本例中基本上是Babel生成的。问题是Map包括V8的实现过于严格,并检查 Map.set.call
中的电话是一个地图,而不是在其原型链中使用地图。
同样适用于Promise。
Trying to get away with custom get/set functionality on ES6 Maps. Currently using Babel to transpile my code to ES5.
Chrome Version 41.0.2272.101 m
class MyMap extends Map {
get(key) {
if (!this.has(key)) { throw new Error(...); }
return super.get(key);
}
set(key) {
if (this.has(key)) { throw new Error(...); }
return super.set(key);
}
}
Not sure if I just got the syntax wrong or I'm missing an implementation of some sort. But I get the following error:
Method Map.prototype.forEach called on incompatible reciever
Babel explictly states they do not support extending built-in classes. See http://babeljs.io/docs/usage/caveats/#classes. The reasons are not quite as simple as "limitations in ES5", however, since Map
is not an ES5 feature to begin with. It appears that implementations of Map do not support basic patterns such as
Map.prototype.set.call(mymap, 'key', 1);
which is essentially what Babel generates in this case. The problem is that implementations of Map including V8 are overly restrictive and check that the this
in the Map.set.call
call is precisely a Map, rather than having Map in its prototype chain.
Same applies to Promise.
这篇关于创建一个从ES6 Map扩展的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!