You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jan 25, 2022. It is now read-only.
Copy file name to clipboardExpand all lines: README.md
+10-34Lines changed: 10 additions & 34 deletions
Original file line number
Diff line number
Diff line change
@@ -7,6 +7,7 @@ Current Stage:
7
7
## Authors
8
8
* Claude Pache (@claudepache)
9
9
* Gabriel Isenberg (@the_gisenberg)
10
+
* Dustin Savery (@dustinsavery)
10
11
11
12
## Overview and motivation
12
13
When looking for a property value that's deep in a tree-like structure, one often has to check whether intermediate nodes exist:
@@ -29,19 +30,6 @@ var street = user.address?.street
29
30
var fooValue =myForm.querySelector('input[name=foo]')?.value
30
31
```
31
32
32
-
The call variant of Optional Chaining is useful for dealing with interfaces that have optional methods:
33
-
34
-
```js
35
-
iterator.return?.() // manually close an iterator
36
-
```
37
-
or with methods not universally implemented:
38
-
```js
39
-
if (myForm.checkValidity?.() ===false) { // skip the test in older web browsers
40
-
// form validation fails
41
-
return;
42
-
}
43
-
```
44
-
45
33
## Prior Art
46
34
The following languages implement the operator with the same general semantics as this proposal (i.e., 1) guarding against a null base value, and 2) short-circuiting application to the whole chain):
47
35
* C#: [Null-conditional operator](https://msdn.microsoft.com/en-us/library/dn986595.aspx) — null-conditional member access or index, in read access.
@@ -58,7 +46,6 @@ The Optional Chaining operator is spelled `?.`. It may appear in three positions
58
46
```javascript
59
47
obj?.prop// optional static property access
60
48
obj?.[expr] // optional dynamic property access
61
-
func?.(...args) // optional function or method call
62
49
```
63
50
64
51
### Notes
@@ -79,11 +66,7 @@ a == null ? undefined : a[x]
79
66
80
67
a?.b() // undefined if `a` is null/undefined
81
68
a ==null?undefined:a.b() // throws a TypeError if `a.b` is not a function
82
-
// otherwise, evaluates to `a.b()`
83
-
84
-
a?.() // undefined if `a` is null/undefined
85
-
a ==null?undefined:a() // throws a TypeError if `a` is neither null/undefined, nor a function
86
-
// invokes the function `a` otherwise
69
+
// otherwise, evaluates to `a.b()`
87
70
```
88
71
89
72
### Short-circuiting
@@ -116,9 +99,9 @@ Let’s call *Optional Chain* an Optional Chaining operator followed by a chain
116
99
An Optional Chain may be followed by another Optional Chain.
117
100
118
101
```js
119
-
a?.b[3].c?.(x).d
120
-
a ==null?undefined:a.b[3].c==null?undefined:a.b[3].c(x).d
121
-
// (as always, except that `a` and `a.b[3].c` are evaluated only once)
102
+
a?.b?.['foo'].c(x).d
103
+
a ==null?undefined:a.b['foo']==null?undefined:a.b['foo'].c(x).d
104
+
// (as always, except that `a` and `a.b['foo']` are evaluated only once)
122
105
```
123
106
124
107
### Edge case: grouping
@@ -134,19 +117,12 @@ That follows from the design choice of specifying the scope of short-circuiting
134
117
135
118
Note that, whatever the semantics are, there is no practical reason to use parentheses in that position anyway.
136
119
137
-
### Optional deletion
138
-
139
-
Because the `delete` operator is very liberal in what it accepts, we have that feature for free:
140
-
```js
141
-
delete a?.b
142
-
// delete (a == null ? undefined : a.b) // that *would* work if `? :` could return a Reference...
143
-
a ==null?undefined:deletea.b// this is what we get, really
144
-
```
145
-
146
120
## Not supported
147
121
148
122
Although they could be included for completeness, the following are not supported due to lack of real-world use cases or other compelling reasons; see [Issue # 22](https://github.com/tc39/proposal-optional-chaining/issues/22) and [Issue #54](https://github.com/tc39/proposal-optional-chaining/issues/54) for discussion:
149
123
124
+
* optional function execution: `a?.()`
125
+
* optional deletion: `delete a?.b`
150
126
* optional construction: `newa?.()`
151
127
* optional template literal: ``a?.`{b}```
152
128
* constructor or template literals in/after an Optional Chain: `newa?.b()`, ``a?.b`{c}```
@@ -165,16 +141,16 @@ All the above cases will be forbidden by the grammar or by static semantics so t
165
141
<dl>
166
142
167
143
168
-
<dt>obj?.[expr] and func?.(arg) look ugly. Why not use obj?[expr] and func?(arg) as does <language X>?
144
+
<dt>obj?.[expr] looks ugly. Why not use obj?[expr] as does <language X>?
169
145
170
146
<dd>
171
147
172
-
We don’t use the `obj?[expr]`and `func?(arg)`syntax, because of the difficulty for the parser to efficiently distinguish those forms from the conditional operator, e.g., `obj?[expr].filter(fun):0` and `func?(x -2) +3:1`.
148
+
We don’t use the `obj?[expr]` syntax, because of the difficulty for the parser to efficiently distinguish those forms from the conditional operator, e.g., `obj?[expr].filter(fun):0`.
173
149
174
150
Alternative syntaxes for those two cases each have their own flaws; and deciding which one looks the least bad is mostly a question of personal taste. Here is how we made our choice:
175
151
176
152
* pick the best syntax for the `obj?.prop` case, which is expected to occur most often;
177
-
* extend the use of the recognisable `?.` sequence of characters to other cases: `obj?.[expr]`, `func?.(arg)`.
153
+
* extend the use of the recognisable `?.` sequence of characters to other cases: `obj?.[expr]`.
178
154
179
155
As for <language X>, it has different syntactical constraints than JavaScript because of <some construct not supported by X or working differently in X>.
0 commit comments