Null-Coalescing运算符(??)如何在Spider中工作?

时间:2022-04-23 11:47:09

Spider's null-coalescing operator ?? returns the right expression if the left expression is null or undefined.

蜘蛛的无效合并算子?如果左表达式为null或未定义,则返回正确的表达式。

var name = options.name ?? "default name";

How does it work?

它是如何工作的?

1 个解决方案

#1


5  

The null-coalescing operator usually works with a simple conditional expression. For example, this code in Spider:

空合并运算符通常使用简单的条件表达式。例如,Spider中的这段代码:

var name = options.name ?? "default name";

compiles to the following JavaScript:

编译为以下JavaScript:

var name = options.name == null ? "default name" : options.name;

(more about equals equals null)

(更多关于equals等于null)

Undefined Identifier Problem

未定义的标识符问题

Note that if the left expression is an identifier, and that identifier is undefined, then the JS interpreter would raise an error. To solve this, the Spider compiler adds an undefined check. For example this code in Spider:

请注意,如果左表达式是标识符,并且该标识符未定义,则JS解释器将引发错误。为解决此问题,Spider编译器添加了一个未定义的检查。例如Spider中的这段代码:

var name = name ?? "value";

compiles to the following code in JS:

编译为JS中的以下代码:

var name = typeof name === "undefined" || name == null ? "value" : name;

Note that if you want to do something like options.name ?? "default" and you're not sure if options is defined or not, you can use the null propagating operator:

请注意,如果你想做options.name ?? “default”并且您不确定是否定义了选项,您可以使用null传播运算符:

var name = options?.name ?? "default";

Double Function Call Problem

双重函数调用问题

If the left expression is a call expression (e.g fn()), then it might get called twice - the first time for the null test, and the second time for the value. To solve this, the Spider compiler moves the call expression to a different variable. For example this code in Spider:

如果左表达式是一个调用表达式(例如fn()),那么它可能会被调用两次 - 第一次用于null测试,第二次用于值。为了解决这个问题,Spider编译器将调用表达式移动到另一个变量。例如Spider中的这段代码:

var name = getName() ?? "default name";

is compiled into something like:

编译成如下:

var tmp = getName();
var name = tmp == null ? "default name" : tmp;

Statement Problem

If the null-coalescing operator is used as a statement and not as an expression, for example:

如果null-coalescing运算符用作语句而不是表达式,例如:

a() ?? b();

then the Spider compiler uses an if statement instead of a conditional expression:

然后Spider编译器使用if语句而不是条件表达式:

if (a() == null) {
  b();
}

#1


5  

The null-coalescing operator usually works with a simple conditional expression. For example, this code in Spider:

空合并运算符通常使用简单的条件表达式。例如,Spider中的这段代码:

var name = options.name ?? "default name";

compiles to the following JavaScript:

编译为以下JavaScript:

var name = options.name == null ? "default name" : options.name;

(more about equals equals null)

(更多关于equals等于null)

Undefined Identifier Problem

未定义的标识符问题

Note that if the left expression is an identifier, and that identifier is undefined, then the JS interpreter would raise an error. To solve this, the Spider compiler adds an undefined check. For example this code in Spider:

请注意,如果左表达式是标识符,并且该标识符未定义,则JS解释器将引发错误。为解决此问题,Spider编译器添加了一个未定义的检查。例如Spider中的这段代码:

var name = name ?? "value";

compiles to the following code in JS:

编译为JS中的以下代码:

var name = typeof name === "undefined" || name == null ? "value" : name;

Note that if you want to do something like options.name ?? "default" and you're not sure if options is defined or not, you can use the null propagating operator:

请注意,如果你想做options.name ?? “default”并且您不确定是否定义了选项,您可以使用null传播运算符:

var name = options?.name ?? "default";

Double Function Call Problem

双重函数调用问题

If the left expression is a call expression (e.g fn()), then it might get called twice - the first time for the null test, and the second time for the value. To solve this, the Spider compiler moves the call expression to a different variable. For example this code in Spider:

如果左表达式是一个调用表达式(例如fn()),那么它可能会被调用两次 - 第一次用于null测试,第二次用于值。为了解决这个问题,Spider编译器将调用表达式移动到另一个变量。例如Spider中的这段代码:

var name = getName() ?? "default name";

is compiled into something like:

编译成如下:

var tmp = getName();
var name = tmp == null ? "default name" : tmp;

Statement Problem

If the null-coalescing operator is used as a statement and not as an expression, for example:

如果null-coalescing运算符用作语句而不是表达式,例如:

a() ?? b();

then the Spider compiler uses an if statement instead of a conditional expression:

然后Spider编译器使用if语句而不是条件表达式:

if (a() == null) {
  b();
}