[英]Classes with static arrow functions
I'm currently implementing the static land specification (an alternative of fantasy land). 我目前正在实施静态土地规范(幻想土地的另一种选择)。 I want to not only use plain objects as types but also ES2015 classes with static methods.
我不仅要使用普通对象作为类型,还要使用静态方法的ES2015类。 I've implemented these static methods as arrow functions in curried form instead of normal functions.
我已经将这些静态方法实现为curry形式的箭头函数而不是普通函数。 However, this isn't possible with ES2015 classes:
但是,ES2015类无法实现这一点:
class List extends Array {
static map = f => xs => xs.map(x => f(x))
static of = x => [x]
}
My map
doesn't need its own this
, because it is merely a curried function on the List
constructor. 我的
map
不需要它自己的this
,因为它只是List
构造函数的curried函数。 To make it work I have to write static map(f) { return xs => xs.map(x => f(x)) }
, what is very annoying. 为了使它工作,我必须写
static map(f) { return xs => xs.map(x => f(x)) }
,这是非常烦人的。
Why can't I use arrow functions along with an assignment expression in ES2015 classes?
为什么我不能在ES2015类中使用箭头函数和赋值表达式?
Because that's just not how the ES2015 class syntax is designed — for now , see under the line below. 因为这不是ES2015类语法的设计方式 - 现在 ,请参阅下面的行。
Is there a concise way to achieve my goal anyway?
有没有简洁的方法来实现我的目标?
It's not clear to me that you want classes at all, just an object: 我不清楚你想要课程,只是一个对象:
const List = { map: f => xs => xs.map(x => f(x)), of: x => [x] };
(You've said that extending is important to what you're doing.) (你已经说过扩展对你正在做的事情很重要。)
But if you want List
to extend Array
(eg, you will have instances) but then add these statics to it, you'll need a two-step: 但是如果你想让
List
扩展Array
(例如,你将有实例)但是然后将这些静态添加到它,你需要两个步骤:
let List = Object.assign( class List extends Array { }, { map: f => xs => xs.map(x => f(x)), of: x => [x] } ); console.log(List.of(42)); // [42]
If you want them non-enumerable or non-configurable, etc., you'll want Object.defineProperties
rather than Object.assign
; 如果你想要它们是不可枚举的或不可配置的等等,你需要
Object.defineProperties
而不是Object.assign
; I'll leave that as an exercise for the reader... 我会把它作为读者的练习......
There's a Stage 3 proposal for class "fields," including static fields, which is being actively implemented by JavaScript engine builders. 有一个阶段 “字段”的第3阶段提案 ,包括静态字段,由JavaScript引擎构建器主动实现。 (And you can use it now via tools like Babel .) It provides static field declaration syntax within the class, almost exactly the way you showed them:
(你现在可以通过像Babel这样的工具使用它。)它在类中提供了静态字段声明语法,几乎就是你展示它们的方式:
// Not in the language yet, but at Stage 3 and shipping without // any flags in V8 (for instance, in Chrome) class List extends Array { static map = f => xs => xs.map(x => f(x)); static of = x => [x]; } console.log(List.of(42)); // [42]
Note: There's a standard Array.of
method, so I wouldn't add an incompatible of
to that List
. 注意:有一个标准的
Array.of
方法,所以我不会添加与该List
不兼容of
方法。
Finally, I should note that unless there's some reason they have to be arrow functions, ES2015's class
syntax supports static methods: 最后,我应该注意,除非有某些原因它们必须是箭头函数,ES2015的
class
语法支持静态方法:
// ES2015+ class List extends Array { static map(f) { return xs => xs.map(x => f(x)); } static of(x) { return [x]; } } console.log(List.of(42)); // [42]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.