简体   繁体   English

具有静态箭头功能的类

[英]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类中使用箭头函数和赋值表达式?
  • Is there a concise way to achieve my goal anyway? 有没有简洁的方法来实现我的目标?

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.

相关问题 ES6 Javascript:使用箭头函数从类中调用静态方法 - ES6 Javascript: Calling static methods from classes with arrow functions 是否可以在 ES6 的类中使用箭头函数? - Is it possible to use arrow functions in classes with ES6? Inheritance 和 JavaScript 类中使用箭头函数的多态性 - Inheritance and polymorphism using arrow functions in JavaScript Classes 在JavaScript类中混合箭头功能是否有任何弊端? - Are there any drawbacks to mixing arrow functions in javascript classes? 为什么作为箭头函数的方法在React类中起作用而在普通类中不起作用? - Why methods as arrow functions work in react classes but not in normal classes? JS 类与普通对象中的箭头函数有何不同 - How arrow functions differ in JS Classes vs Normal objects 在课程中使用Ecmascript 6箭头函数作为方法的正确方法是什么? - What is the right way to use Ecmascript 6 arrow functions as methods in classes? JS:不确定 static 用于类中的函数 - JS: Not sure the use of static for functions in classes 在函数声明的类中声明 static 方法 - Declaring static methods inside of classes declared by functions 为什么箭头函数作为静态成员值没有词法范围? - Why are arrow functions as static members values not lexically scoped?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM