具有静态箭头功能的类 [英] Classes with static arrow functions

查看:88
本文介绍了具有静态箭头功能的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在实施静态土地规范(幻想土地的另一种选择)。我不仅要使用普通对象作为类型,还要使用静态方法的ES2015类。我已经将这些静态方法实现为curry形式的箭头函数而不是普通函数。但是,ES2015课程无法做到这一点:

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. I've implemented these static methods as arrow functions in curried form instead of normal functions. However, this isn't possible with ES2015 classes:

class List extends Array {
  static map = f => xs => xs.map(x => f(x))
  static of = x => [x]
}

我的地图不需要自己的这个,因为它只是 List 构造函数的curried函数。为了使它工作,我必须写静态地图(f){return xs => xs.map(x => f(x))} ,非常烦人。

My map doesn't need its own this, because it is merely a curried function on the List constructor. To make it work I have to write static map(f) { return xs => xs.map(x => f(x)) }, what is very annoying.


  • 为什么我不能在ES2015课程中使用箭头函数和赋值表达式吗?

  • 有没有简洁的方法来实现我的目标?

推荐答案


为什么我不能在ES2015课程中使用箭头函数和赋值表达式?

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.


是否有简洁的方法来实现我的目标?

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]
};

(你说过扩展对你所做的很重要这样做。)

但如果你想要 List 来扩展 Array (例如,你将有实例),但随后将这些静态添加到它,你需要两步:

But if you want List to extend Array (e.g., you will have instances) but then add these statics to it, you'll need a two-step:

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 ;我将把它作为读者的练习...

If you want them non-enumerable or non-configurable, etc., you'll want Object.defineProperties rather than Object.assign; I'll leave that as an exercise for the reader...

有一个第3阶段提案,用于类字段,包括静态字段,由JavaScript引擎构建器主动实现。 (现在可以通过 Babel 等工具使用它。)它在类中提供静态字段声明语法,几乎完全是你展示它们的方式:

There's a 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.) It provides static field declaration syntax within the class, almost exactly the way you showed them:

// 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 <中添加不兼容的 / code>。

Note: There's a standard Array.of method, so I wouldn't add an incompatible of to that List.

最后,我应该注意,除非他们成为箭头功能,否则ES2015的 class 语法支持静态方法:

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 List extends Array {
  static map(f) {
    return xs => xs.map(x => f(x));
  }
  static of(x) {
    return [x];
  }
}

console.log(List.of(42)); // [42]

这篇关于具有静态箭头功能的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆