Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES2020: Nullish coalescing Operator #1178

Closed
2 tasks
azu opened this issue Jun 20, 2020 · 4 comments · Fixed by #1205
Closed
2 tasks

ES2020: Nullish coalescing Operator #1178

azu opened this issue Jun 20, 2020 · 4 comments · Fixed by #1205

Comments

@azu
Copy link
Collaborator

azu commented Jun 20, 2020

Null合体演算子 - JavaScript | MDN
合体という訳は微妙な気がするけど、これはデフォルト引数の || の問題を解決する話、falsyの話と関係するので入れるのは妥当な気がする。

https://jsprimer.net/basic/function-declaration/#function-default-parameters

falsyの話題自体はif文と関係するから残るはず。
|| での判定の問題にフォーカスするのがいい気がする。
デフォルト引数の解決のパターンとして並べるのが妥当かもしれない?
演算子に入るイメージはあんまりないのかも。演算子が肥大化するから、一旦演算子の章を整理して必須じゃないものは全部消すとかしてもいい。

やること

  • 呼び方を決める
  • どこに追加をするかを決める
This was referenced Jun 20, 2020
@azu
Copy link
Collaborator Author

azu commented Jun 28, 2020

“nullish” values (null or undefined)
-- https://www.ecma-international.org/ecma-262/11.0/index.html

ES2020 から nullish という概念が増えている。
falsyと同じようにこの言葉についても解説が必要

ES2019では nullish はでてこない。
ECMAScript® 2019 Language Specification

@azu
Copy link
Collaborator Author

azu commented Jun 28, 2020

Null合体演算子

少なくても null じゃなくて nullish なので この訳語にはできない気がする。
Nullish coalescing演算子かNullish coalescing operatorになる気がする

@azu
Copy link
Collaborator Author

azu commented Jun 28, 2020

tc39/proposal-logical-assignment: A proposal to combine Logical Operators and Assignment Expressions
に対して対応する場合も考える。

Nullish coalescingはLogicalとついてはいないけど、実質Logicalの分類に近い気がする。

https://tc39.es/ecma262/#sec-binary-logical-operators-static-semantics-isfunctiondefinition

なので、

でよさそう。

演算子(Nullish) → オブジェクト(Optional chaining) → 関数(Nullish coalescing)という順番になる。

これ系の対応

  • default parameter: undefined
  • nullish: undefined or null
  • falsy: false, undefined, null, 0, NaN, ""

@azu
Copy link
Collaborator Author

azu commented Jul 5, 2020

意外にもユースケースでは || を使ってない。
const count = this.items.get(item) || 0;?? の方が適切なので、コメント付きで書き換える
https://jsprimer.net/basic/map-and-set/#object-and-map

@azu azu closed this as completed in #1205 Aug 29, 2020
azu added a commit that referenced this issue Aug 29, 2020
Nullish coalescing演算子(`??`)とOptional chaining(`?.`)に関する変更

## 変更点

- [x] **falsy**の説明を演算子の章に移動
  - falsyの対応としてnullishを演算子の章で解説するため
  - BigIntの `0n` も falsy に追加 #445 
- [x] Optional chaining演算子の(`?.`)の解説を"オブジェクト"の章に追加
- [x] Nullish coalescing演算子(`??`)とOptional chaining(`?.`)の組み合わせを説明
- [x] Nullish coalescing演算子(`??`)の解説を"演算子"の章に追加
- [x] 一部のコードを `||` を `??` に置き換え
  - 例としては問題ないけど、`??` 推奨気味に変更

## 追加しなかったこと

- デフォルト値に対するNullish coalescing演算子(`??`)とOptional chaining(`?.`)の組み合わせのパターン
  - 他にもいろいろな書き方があるため含めないようにした
- 関数呼び出しとOptional chaining演算子(`?.`)
  - `window.fn?.()` みたいなケース
  - ユースケースがイマイチ。別の解決方法でも良いと思える気がする

fix #1178 
fix #1179
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant