3 回答

TA贡献1876条经验 获得超5个赞
根据您的示例,假设您想要一个匿名函数调用。如果是这样,是的,您可以使用IIFE执行此操作:
public startDate = this.formatDate(
(() => {
// do some other stuff
return new Date(/* ...args */)
})()
)
但是,为函数命名通常更利于可读性。

TA贡献1873条经验 获得超9个赞
type GetDate = () => Date
const formatDate = (getDate: GetDate) => {
const date = getDate()
// formate date
}
// usage
const getDateFromInternet = () => API.getDate() // imaginary API which fetches date
const formattedDate = formatDate(getDateFromInternet)

TA贡献1876条经验 获得超6个赞
class A {
public startDate = this.dateFactory();
constructor(
private dateFactory,
) {}
...
}
// somewhere in code
new A(() => new Date());
添加回答
举报