类 带有静态箭头函数
Classes with static arrow functions
我目前正在实施 static land 规范(幻想世界的另一种选择)。我不仅想将普通对象用作类型,而且还想将 ES2015 类 与静态方法一起使用。我已经将这些静态方法实现为柯里化形式的箭头函数,而不是普通函数。但是,这对于 ES2015 类:
是不可能的
class List extends Array {
static map = f => xs => xs.map(x => f(x))
static of = x => [x]
}
我的 map
不需要它自己的 this
,因为它只是 List
构造函数上的柯里化函数。为了让它工作,我必须写 static map(f) { return xs => xs.map(x => f(x)) }
,这很烦人。
- 为什么我不能在 ES2015 中将箭头函数与赋值表达式一起使用 类?
- 有没有一种简洁的方法可以实现我的目标?
Why can't I use arrow functions along with an assignment expression in ES2015 classes?
因为那不是 ES2015 class 语法的设计方式——目前,请参阅下面的行。
Is there a concise way to achieve my goal anyway?
我不清楚你想要 classes,只是一个对象:
const List = {
map: f => xs => xs.map(x => f(x)),
of: x => [x]
};
(你说过扩展对你所做的事情很重要。)
但是,如果您希望 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]
如果您希望它们不可枚举或不可配置等,您需要 Object.defineProperties
而不是 Object.assign
;我将把它留作 reader...
的练习
有一个 Stage 3 proposal for class "fields," including static fields, which is being actively implemented by JavaScript engine builders. (And you can use it now via tools like Babel。)它在 class 中提供了静态字段声明语法,几乎与您展示的完全一样:
// 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]
注意: 有一个标准的 Array.of
方法,所以我不会在 List
中添加不兼容的 of
。
最后,我要指出,除非有某种原因它们 必须 是箭头函数,否则 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]
我目前正在实施 static land 规范(幻想世界的另一种选择)。我不仅想将普通对象用作类型,而且还想将 ES2015 类 与静态方法一起使用。我已经将这些静态方法实现为柯里化形式的箭头函数,而不是普通函数。但是,这对于 ES2015 类:
是不可能的class List extends Array {
static map = f => xs => xs.map(x => f(x))
static of = x => [x]
}
我的 map
不需要它自己的 this
,因为它只是 List
构造函数上的柯里化函数。为了让它工作,我必须写 static map(f) { return xs => xs.map(x => f(x)) }
,这很烦人。
- 为什么我不能在 ES2015 中将箭头函数与赋值表达式一起使用 类?
- 有没有一种简洁的方法可以实现我的目标?
Why can't I use arrow functions along with an assignment expression in ES2015 classes?
因为那不是 ES2015 class 语法的设计方式——目前,请参阅下面的行。
Is there a concise way to achieve my goal anyway?
我不清楚你想要 classes,只是一个对象:
const List = {
map: f => xs => xs.map(x => f(x)),
of: x => [x]
};
(你说过扩展对你所做的事情很重要。)
但是,如果您希望 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]
如果您希望它们不可枚举或不可配置等,您需要 Object.defineProperties
而不是 Object.assign
;我将把它留作 reader...
有一个 Stage 3 proposal for class "fields," including static fields, which is being actively implemented by JavaScript engine builders. (And you can use it now via tools like Babel。)它在 class 中提供了静态字段声明语法,几乎与您展示的完全一样:
// 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]
注意: 有一个标准的 Array.of
方法,所以我不会在 List
中添加不兼容的 of
。
最后,我要指出,除非有某种原因它们 必须 是箭头函数,否则 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]