| 标题 | 标签 |
|---|---|
| coalesce(第一个非 null 和 undefined 的函数) | type,beginner(类型,初学者) |
返回第一个非null和undefined参数的函数。
- 使用
Array.prototype.find()和Array.prototype.includes()查找第一个不等于undefined或null的值。
代码如下:
const coalesce = (...args) => args.find(v => ![undefined, null].includes(v));ts 代码如下:
调用方式:
coalesce(null, undefined, '', NaN, 'Waldo'); // ''应用场景
以下是一个实战示例:
结果如下:
<iframe src="codes/javascript/html/coalesce.html"></iframe>