Javascript中是否有空合并运算符?
例如,在C#中,我可以这样做:
String someString = null;
var whatIWant = someString ?? "Cookies!";
我可以为Javascript找到的最佳近似是使用条件运算符:
var someString = null;
var whatIWant = someString ? someString : 'Cookies!';
这有点讨厌IMHO。我能做得更好吗?
Javascript中是否有空合并运算符?
例如,在C#中,我可以这样做:
String someString = null;
var whatIWant = someString ?? "Cookies!";
我可以为Javascript找到的最佳近似是使用条件运算符:
var someString = null;
var whatIWant = someString ? someString : 'Cookies!';
这有点讨厌IMHO。我能做得更好吗?
当前回答
是的,它很快就要来了。请参阅此处的提案和此处的实施状态。
它看起来像这样:
x ?? y
实例
const response = {
settings: {
nullValue: null,
height: 400,
animationDuration: 0,
headerText: '',
showSplashScreen: false
}
};
const undefinedValue = response.settings?.undefinedValue ?? 'some other default'; // result: 'some other default'
const nullValue = response.settings?.nullValue ?? 'some other default'; // result: 'some other default'
const headerText = response.settings?.headerText ?? 'Hello, world!'; // result: ''
const animationDuration = response.settings?.animationDuration ?? 300; // result: 0
const showSplashScreen = response.settings?.showSplashScreen ?? true; // result: false
其他回答
如果||作为C#??的替代品??在您的情况下还不够好,因为它包含空字符串和零,所以您可以始终编写自己的函数:
function $N(value, ifnull) {
if (value === null || value === undefined)
return ifnull;
return value;
}
var whatIWant = $N(someString, 'Cookies!');
它有望很快在Javascript中提供,因为截至2020年4月,它处于提案阶段。您可以在此处监视状态以获得兼容性和支持-https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator
对于使用Typescript的用户,您可以使用Typescript 3.7中的零合并运算符
从文档中-
你可以想到这个功能-??操作员-作为“跌倒”的方式当处理null或undefined时,返回到默认值。当我们编写类似代码让x=foo??bar();这是一种新的说法,即当foo值为“present”时将使用它;但当它为空或未定义时,在其位置计算bar()。
ECMAScript 2021启用了两个新功能:
空合并运算符(??),是一种逻辑运算符,当左侧操作数为空或未定义时,返回右侧操作数,否则返回左侧操作数。
设b=未定义??5.console.log(b);//5.
逻辑零赋值(x??=y)运算符,仅在x具有零值(空或未定义)时赋值。
常量车={速度:20};车速??=5.控制台日志(轿厢速度);汽车名称??=“雷诺”;console.log(car.name);
有关逻辑零赋值的更多信息,请参阅此处https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_nullish_assignment
有关空合并运算符的更多信息,请参阅此处https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator
是的,它很快就要来了。请参阅此处的提案和此处的实施状态。
它看起来像这样:
x ?? y
实例
const response = {
settings: {
nullValue: null,
height: 400,
animationDuration: 0,
headerText: '',
showSplashScreen: false
}
};
const undefinedValue = response.settings?.undefinedValue ?? 'some other default'; // result: 'some other default'
const nullValue = response.settings?.nullValue ?? 'some other default'; // result: 'some other default'
const headerText = response.settings?.headerText ?? 'Hello, world!'; // result: ''
const animationDuration = response.settings?.animationDuration ?? 300; // result: 0
const showSplashScreen = response.settings?.showSplashScreen ?? true; // result: false
使现代化
JavaScript现在支持空值合并运算符(??)。当左侧操作数为空或未定义时,它返回右侧操作数,否则返回左侧操作数。
旧答案
使用前请检查兼容性。
与C#空合并运算符(??)等效的JavaScript使用逻辑OR(||):
var whatIWant = someString || "Cookies!";
在某些情况下(在下文中进行了澄清),这种行为与C#的行为不匹配,但这是在JavaScript中分配默认值/替代值的通用、简洁的方法。
澄清
无论第一个操作数的类型如何,如果将其转换为布尔值结果为false,则赋值将使用第二个操作数。注意以下所有情况:
alert(Boolean(null)); // false
alert(Boolean(undefined)); // false
alert(Boolean(0)); // false
alert(Boolean("")); // false
alert(Boolean("false")); // true -- gotcha! :)
这意味着:
var whatIWant = null || new ShinyObject(); // is a new shiny object
var whatIWant = undefined || "well defined"; // is "well defined"
var whatIWant = 0 || 42; // is 42
var whatIWant = "" || "a million bucks"; // is "a million bucks"
var whatIWant = "false" || "no way"; // is "false"