📝 update tutorial about new operators

This commit is contained in:
ValKmjolnir 2024-06-06 23:45:27 +08:00
parent f62d747b23
commit b09e2a7875
2 changed files with 40 additions and 0 deletions

View File

@ -164,6 +164,27 @@ a &= 0xca;
a |= 0xba;
```
Operator `??` is used to check left hand side value is `nil` or not, if not,
return the right hand side value:
```javascript
print(nil??"should get this string");
```
The example above will print `should get this string`.
Operator `?.` is used to get hash member if left hand side value is not `nil`.
And if left hand side value is not `nil` and not `hash`,
will cause error and exit.
```javascript
var a = nil;
print(a?.try_get); # nil
var a = {try_get: "congrats!"};
print(a?.try_get); # "congrats!"
```
## Definition
As follows.

View File

@ -159,6 +159,25 @@ a &= 0xca;
a |= 0xba;
```
`??` 运算符用于检查左侧值是否为 `nil`,如果不是则返回右侧的值:
```javascript
print(nil??"should get this string");
```
上面的例子会输出 `should get this string`
`?.` 运算符用于先检查左侧不为 `nil`,如果左侧不是 `nil` 则尝试获取 hash 的字段。
当然如果左侧此时也不是 `hash` 类型,则报错退出。
```javascript
var a = nil;
print(a?.try_get); # nil
var a = {try_get: "congrats!"};
print(a?.try_get); # "congrats!"
```
## 定义变量
如下所示。