有没有把"undefined"改成"null"的JavaScript成语?
Is there a JavaScript idiom to change "undefined" to "null"?
有相当多的 JavaScript 习语在类型和类似事物之间进行强制转换。
!
可以将任何假值转换为布尔值 true
,!!
可以将任何假值转换为实际布尔值 false
,+
可以将 true
、false
,或将表示数字的字符串转换为实际数字等
是否有类似的东西可以将 undefined
转换为 null
?
现在我正在使用三进制 ? :
,但如果知道我是否遗漏了一个有用的技巧会很酷。
好的,让我设计一个例子...
function callback(value) {
return value ? format(value) : null;
}
callback
由 3rd 方代码调用,有时会传递 undefined
.
第 3 方代码可以处理 null
被传回,但不能处理 undefined
。 format()
也是第 3 方,无法处理通过 undefined
或 null
。
undefined || null
- 或任何错误的 || null - 将 return null
Javascript 现在支持 null-coalescing 运算符:??
。它可能不是 production-ready(请参阅 support table),但与 Node 或转译器(TypeScript、Babel 等)一起使用肯定是安全的。
每 MDN,
The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
就像 ||
可以在左操作数为假时提供“默认”值一样,??
在左操作数为空或未定义时提供“默认”值。您可以使用它来将 undefined 强制为 null:
// OR operator can coerce 'defined' values
"value" || null; // "value"
0 || null; // null
false || null; // null
"" || null; // null
undefined || null; // null
// The null-coalescing operator will only coerce undefined or null
"value" ?? null; // "value"
0 ?? null; // 0
false ?? null; // false
"" ?? null; // ""
undefined ?? null; // null
基于问题的示例:
function mustNotReturnUndefined(mightBeUndefined) { // can return null
// Substitute empty string for null or undefined
let result = processValue(mightBeUndefined ?? "");
// Substitute null for undefined
return result ?? null;
}
这是一个相当老的问题,可能我的回答有点晚了,但我自己决定如下:
const valueOrNull = (value = null) => value;
const a = { d: '' };
valueOrNull(a.b?.c) === null; // true
valueOrNull(a.d) === ''; // true
valueOrNull() === null; // true
任何undefined
值将得到null
作为默认值;
public static replaceUndefinedWithNull(object: any) {
if (isUndefined(object)) {
return null;
}
return object;
}
有相当多的 JavaScript 习语在类型和类似事物之间进行强制转换。
!
可以将任何假值转换为布尔值 true
,!!
可以将任何假值转换为实际布尔值 false
,+
可以将 true
、false
,或将表示数字的字符串转换为实际数字等
是否有类似的东西可以将 undefined
转换为 null
?
现在我正在使用三进制 ? :
,但如果知道我是否遗漏了一个有用的技巧会很酷。
好的,让我设计一个例子...
function callback(value) {
return value ? format(value) : null;
}
callback
由 3rd 方代码调用,有时会传递 undefined
.
第 3 方代码可以处理 null
被传回,但不能处理 undefined
。 format()
也是第 3 方,无法处理通过 undefined
或 null
。
undefined || null
- 或任何错误的 || null - 将 return null
Javascript 现在支持 null-coalescing 运算符:??
。它可能不是 production-ready(请参阅 support table),但与 Node 或转译器(TypeScript、Babel 等)一起使用肯定是安全的。
每 MDN,
The nullish coalescing operator (??) is a logical operator that returns its right-hand side operand when its left-hand side operand is null or undefined, and otherwise returns its left-hand side operand.
就像 ||
可以在左操作数为假时提供“默认”值一样,??
在左操作数为空或未定义时提供“默认”值。您可以使用它来将 undefined 强制为 null:
// OR operator can coerce 'defined' values
"value" || null; // "value"
0 || null; // null
false || null; // null
"" || null; // null
undefined || null; // null
// The null-coalescing operator will only coerce undefined or null
"value" ?? null; // "value"
0 ?? null; // 0
false ?? null; // false
"" ?? null; // ""
undefined ?? null; // null
基于问题的示例:
function mustNotReturnUndefined(mightBeUndefined) { // can return null
// Substitute empty string for null or undefined
let result = processValue(mightBeUndefined ?? "");
// Substitute null for undefined
return result ?? null;
}
这是一个相当老的问题,可能我的回答有点晚了,但我自己决定如下:
const valueOrNull = (value = null) => value;
const a = { d: '' };
valueOrNull(a.b?.c) === null; // true
valueOrNull(a.d) === ''; // true
valueOrNull() === null; // true
任何undefined
值将得到null
作为默认值;
public static replaceUndefinedWithNull(object: any) {
if (isUndefined(object)) {
return null;
}
return object;
}