diff --git a/docs/object.md b/docs/object.md index 4ff75b6..74748c0 100644 --- a/docs/object.md +++ b/docs/object.md @@ -698,3 +698,79 @@ let runtimeError = { }; ``` +## Null 判断运算符 + +读取对象属性的时候,如果某个属性的值是`null`或`undefined`,有时候需要为它们指定默认值。常见做法是通过`||`运算符指定默认值。 + +```javascript +const headerText = response.settings.headerText || 'Hello, world!'; +const animationDuration = response.settings.animationDuration || 300; +const showSplashScreen = response.settings.showSplashScreen || true; +``` + +上面的三行代码都通过`||`运算符指定默认值,但是这样写是错的。开发者的原意是,只要属性的值为`null`或`undefined`,默认值就会生效,但是属性的值如果为空字符串或`false`或`0`,默认值也会生效。 + +为了避免这种情况,[ECMAScript2020](https://github.com/tc39/proposal-nullish-coalescing) 引入了一个新的 Null 判断运算符`??`。它的行为类似`||`,但是只有运算符左侧的值为`null`或`undefined`时,才会返回右侧的值。 + +```javascript +const headerText = response.settings.headerText ?? 'Hello, world!'; +const animationDuration = response.settings.animationDuration ?? 300; +const showSplashScreen = response.settings.showSplashScreen ?? true; +``` + +上面代码中,默认值只有在属性值为`null`或`undefined`时,才会生效。 + +这个运算符的一个目的,就是跟链判断运算符`?.`配合使用,为`null`或`undefined`的值设置默认值。 + +```javascript +const animationDuration = response.settings?.animationDuration ?? 300; +``` + +上面代码中,`response.settings`如果是`null`或`undefined`,就会返回默认值300。 + +这个运算符很适合判断函数参数是否赋值。 + +```javascript +function Component(props) { + const enable = props.enabled ?? true; + // … +} +``` + +上面代码判断`props`参数的`enabled`属性是否赋值,等同于下面的写法。 + +```javascript +function Component(props) { + const { + enabled: enable = true, + } = props; + // … +} +``` + +`??`有一个运算优先级问题,它与`&&`和`||`的优先级孰高孰低。现在的规则是,如果多个逻辑运算符一起使用,必须用括号表明优先级,否则会报错。 + +```javascript +// 报错 +lhs && middle ?? rhs +lhs ?? middle && rhs +lhs || middle ?? rhs +lhs ?? middle || rhs +``` + +上面四个表达式都会报错,必须加入表明优先级的括号。 + +```javascript +(lhs && middle) ?? rhs; +lhs && (middle ?? rhs); + +(lhs ?? middle) && rhs; +lhs ?? (middle && rhs); + +(lhs || middle) ?? rhs; +lhs || (middle ?? rhs); + +(lhs ?? middle) || rhs; +lhs ?? (middle || rhs); +``` + diff --git a/docs/proposals.md b/docs/proposals.md index e119a63..a8d40af 100644 --- a/docs/proposals.md +++ b/docs/proposals.md @@ -251,82 +251,6 @@ a?.b = c 为了保证兼容以前的代码,允许`foo?.3:0`被解析成`foo ? .3 : 0`,因此规定如果`?.`后面紧跟一个十进制数字,那么`?.`不再被看成是一个完整的运算符,而会按照三元运算符进行处理,也就是说,那个小数点会归属于后面的十进制数字,形成一个小数。 -## Null 判断运算符 - -读取对象属性的时候,如果某个属性的值是`null`或`undefined`,有时候需要为它们指定默认值。常见做法是通过`||`运算符指定默认值。 - -```javascript -const headerText = response.settings.headerText || 'Hello, world!'; -const animationDuration = response.settings.animationDuration || 300; -const showSplashScreen = response.settings.showSplashScreen || true; -``` - -上面的三行代码都通过`||`运算符指定默认值,但是这样写是错的。开发者的原意是,只要属性的值为`null`或`undefined`,默认值就会生效,但是属性的值如果为空字符串或`false`或`0`,默认值也会生效。 - -为了避免这种情况,现在有一个[提案](https://github.com/tc39/proposal-nullish-coalescing),引入了一个新的 Null 判断运算符`??`。它的行为类似`||`,但是只有运算符左侧的值为`null`或`undefined`时,才会返回右侧的值。 - -```javascript -const headerText = response.settings.headerText ?? 'Hello, world!'; -const animationDuration = response.settings.animationDuration ?? 300; -const showSplashScreen = response.settings.showSplashScreen ?? true; -``` - -上面代码中,默认值只有在属性值为`null`或`undefined`时,才会生效。 - -这个运算符的一个目的,就是跟链判断运算符`?.`配合使用,为`null`或`undefined`的值设置默认值。 - -```javascript -const animationDuration = response.settings?.animationDuration ?? 300; -``` - -上面代码中,`response.settings`如果是`null`或`undefined`,就会返回默认值300。 - -这个运算符很适合判断函数参数是否赋值。 - -```javascript -function Component(props) { - const enable = props.enabled ?? true; - // … -} -``` - -上面代码判断`props`参数的`enabled`属性是否赋值,等同于下面的写法。 - -```javascript -function Component(props) { - const { - enabled: enable = true, - } = props; - // … -} -``` - -`??`有一个运算优先级问题,它与`&&`和`||`的优先级孰高孰低。现在的规则是,如果多个逻辑运算符一起使用,必须用括号表明优先级,否则会报错。 - -```javascript -// 报错 -lhs && middle ?? rhs -lhs ?? middle && rhs -lhs || middle ?? rhs -lhs ?? middle || rhs -``` - -上面四个表达式都会报错,必须加入表明优先级的括号。 - -```javascript -(lhs && middle) ?? rhs; -lhs && (middle ?? rhs); - -(lhs ?? middle) && rhs; -lhs ?? (middle && rhs); - -(lhs || middle) ?? rhs; -lhs || (middle ?? rhs); - -(lhs ?? middle) || rhs; -lhs ?? (middle || rhs); -``` - ## 函数的部分执行 ### 语法