【发布时间】:2021-02-08 13:30:00
【问题描述】:
假设我们有以下示例:
class Person {
surname: string;
firstname: string;
getFullName(separator: string) {
return this.surname + separator + this.firstname;
}
}
const person = ...
const fullName = _.ifNotNull(person, value => value.getFullName('-'));
是否有与Lodash 中的ifNotNull 函数等效的函数,它仅在对象参数不为空或未定义时应用来自对象参数的方法?
【问题讨论】:
-
你不需要lodash。
person?.getFullName('-') -
@MoshFeu 你的意思是
person?person.getFullName('-'):default ?它认为使用像_.ifNotNull这样的函数可以更干净 -
不,他的意思是
person?.getFullName,它叫Optional Chaining。尝试使用函数来实现已经内置的功能似乎毫无意义。它看起来不干净,看起来像是在浪费时间。 -
@DaneBrouwer 好吧,我正在使用需要 TypeScript >=3.4.0 和
标签: typescript lodash