如何自动将参数应用于类构造函数? [英] How to automatically apply argument to class constructor?
本文介绍了如何自动将参数应用于类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个es6类User
和下面给出的全局函数map()
:
I have an es6 class User
and a global function map()
given below:
class User {
constructor(public name: string) {}
}
const map = <T, R>(project: (value: T) => R) => {}
代替编写以下内容:
map((value) => new User(value))
我想(以某种方式)写一些东西:
I want to (somehow) write something like:
map(new User)
我不确定这是否可行.
推荐答案
您可以在您的类中创建一个静态函数,该函数采用值param并返回一个新的User:
You can create a static function in your class that takes the value param and returns a new User:
class User {
static createUser(value) {
return new User(value)
}
}
然后使用:
map(User.createUser)
这篇关于如何自动将参数应用于类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文