Number(id) 或 +id 根据输入值产生不同的值

Number(id) or +id result in different values depending on the input value

id: number;

this.id = +id;

When local id is null then +id results in 0
When local id is undfined then +id results in NaN

Number(id) 而不是 +id

得到的结果相同

如何获取 consistent/same return 值 未成功转换 为数字?

由于 0nullNaNundefined 都是假的,如果它是假的,您可以将值设置为某个值

this.id = (+id) || 0;

console.log( (+0) || 0 )           // 0
console.log( (+null) || 0 )        // 0
console.log( (+undefined) || 0 )   // 0