Baseline Widely available
optional chaining ì°ì°ì (?.
) ë ì²´ì¸ì ê° ì°¸ì¡°ê° ì í¨íì§ ëª
ìì ì¼ë¡ ê²ì¦íì§ ìê³ , ì°ê²°ë ê°ì²´ ì²´ì¸ ë´ì ê¹ìì´ ìì¹í ìì± ê°ì ì½ì ì ìë¤.
?.
ì°ì°ìë .
ì²´ì´ë ì°ì°ìì ì ì¬íê² ìëíì§ë§, ë§ì½ ì°¸ì¡°ê° nullish (null
ëë undefined
)ì´ë¼ë©´, ìë¬ê° ë°ìíë ê² ëì ì ííìì ë¦¬í´ ê°ì undefined
ë¡ ë¨ë½ëë¤. í¨ì í¸ì¶ìì ì¬ì©ë ë, ë§ì½ 주ì´ì§ í¨ìê° ì¡´ì¬íì§ ìëë¤ë©´, undefined
를 리í´íë¤.
ë°ë¼ì ì°¸ì¡°ê° ëë½ë ê°ë¥ì±ì´ ìë ê²½ì° ì°ê²°ë ìì±ì¼ë¡ ì ê·¼í ë ë ì§§ê³ ê°ë¨í ííìì´ ìì±ëë¤. ì´ë¤ ìì±ì´ íìíì§ì ëí ë³´ì¦ì´ íì¤íì§ ìë ê²½ì° ê°ì²´ì ë´ì©ì íìíë ëì ëìì´ ë ì ìë¤.
optional chainingì ì ì¸ëì§ ìì ë£¨í¸ ê°ì²´ì ì¬ì©í ì ìì§ë§, ì ìëì§ ìì ë£¨í¸ ê°ì²´ìë í¨ê» ì¬ì©í ì ìë¤.
ìëí´ ë³´ê¸°const adventurer = {
name: "Alice",
cat: {
name: "Dinah",
},
};
const dogName = adventurer.dog?.name;
console.log(dogName);
// Expected output: undefined
console.log(adventurer.someNonExistentMethod?.());
// Expected output: undefined
문ë²
obj?.prop;
obj?.[expr];
arr?.[index];
func?.(args);
ì¤ëª
optional chaining ì°ì°ìë 참조ë 기ë¥ì´ undefined
ëë null
ì¼ ì ìì ë ì°ê²°ë ê°ì²´ì ê°ì ì ê·¼íë ë¨ìíí ì ìë ë°©ë²ì ì ê³µíë¤.
ì를 ë¤ì´, ì¤ì²©ë 구조를 ê°ì§ ê°ì²´ìì obj
ê° ìë¤. optional chainingì´ ìì´ ê¹ì´ ì¤ì²©ë íì ìì±ì ì°¾ì¼ë ¤ë©´, ë¤ìê³¼ ê°ì´ 참조를 íì¸í´ì¼ íë¤:
let nestedProp = obj.first && obj.first.second;
obj.first
ì ê°ì obj.first.second
ì ê°ì ì ê·¼í기 ì ì null
(ê·¸ë¦¬ê³ undefined
)ê° ìëë¼ë ì ì ê²ì¦íë¤. ì´ë ë§ì½ì obj.first
를 í
ì¤í¸ ìì´ obj.first.second
ì ì§ì ì ê·¼í ë ì¼ì´ë ì ìë ìë¬ë¥¼ ë°©ì§íë¤.
ê·¸ë¬ë optional chaining ì°ì°ì(?.
)를 ì¬ì©íì¬, obj.first.second
ì ì ê·¼í기 ì ì obj.first
ì ìíì ë°ë¼ ëª
ìì ì¼ë¡ í
ì¤í¸íê±°ë ë¨ë½ìí¤ì§ ììë ëë¤:
let nestedProp = obj.first?.second;
.
ëì ì ?.
ì°ì°ì를 ì¬ì©í¨ì¼ë¡ì¨, JavaScriptë obj.first.second
ì ì ê·¼í기 ì ì obj.first
ê° null
ëë undefined
ê° ìëë¼ë ê²ì ì묵ì ì¼ë¡ íì¸íë ê²ì ìê³ ìë¤. ë§ì½ obj.first
ê° null
ëë undefined
ì´ë¼ë©´, ê·¸ ííìì ìëì¼ë¡ ë¨ë½ëì´ undefined
ê° ë°íëë¤.
ì´ë ìì ë³ìê° ì¤ì ë¡ ìì±ëì§ ìëë¤ë ì ì ì ì¸íê³ ë¤ìê³¼ ëì¼íë¤.
let temp = obj.first;
let nestedProp = temp === null || temp === undefined ? undefined : temp.second;
í¨ìì í¸ì¶ê³¼ Optional chaining
ì¡´ì¬íì§ ìì ì ìë 매ìë를 í¸ì¶í ë, optional chainingì ì¬ì©í ì ìë¤. ì를 ë¤ì´, 구í 기ê°ì´ë ì¬ì©ì ì¥ì¹ìì ì¬ì©í ì ìë ê¸°ë¥ ë문ì ë©ìë를 ì¬ì©í ì ìë API를 ì¬ì©í ê²½ì°, ì ì©í ì ìë¤.
í¨ì í¸ì¶ê³¼ optional chainingì ì¬ì©í¨ì¼ë¡ì¨ ë©ìë를 ì°¾ì ì ìë ê²½ì°ì ìì¸ë¥¼ ë°ììí¤ë ê² ëì ì ê·¸ ííìì ìëì¼ë¡ undefined
를 ë°ííë¤:
let result = someInterface.customMethod?.();
ë©ëª¨: ë§ì½ ìì±ì í´ë¹ ì´ë¦ì´ ìì§ë§ í¨ìê° ìëë¼ë©´, ?.
ì ì¬ì©ì ì¬ì í ìì¸ë¥¼ ë°ììí¨ë¤. TypeError
exception (x.y
is not a function
).
ë©ëª¨: ë§ì½ someInterface
ìì²´ê° null
ì´ë undefined
ì´ë©´, TypeError
ìì¸ê° ì¬ì í ë°ì í ê²ì´ë¤. someInterface
ìì²´ê° null
ì´ë undefined
ì¼ë¡ ììëë¤ë©´, ?.
ì ì¬ì©í´ì¼íë¤: someInterface?.customMethod?.()
ë§ì½ ê°ì²´ìì destructuring assignmentë¡ callbacks ëë fetch ë©ìë를 ì¬ì©íë¤ë©´, ê·¸ ì¡´ì¬ ì¬ë¶ë¥¼ í
ì¤í¸íì§ ìì¼ë©´ í¨ìë¡ í¸ì¶í ì ìë ì¡´ì¬ íì§ ìë ê°ì ê°ì§ ì ìë¤. ?.
ì ì¬ì©íë©´, ë¤ì ì¶ê° í
ì¤í¸ë¥¼ í¼í ì ìë¤:
// Written as of ES2019
function doSomething(onContent, onError) {
try {
// ... do something with the data
} catch (err) {
if (onError) {
// Testing if onError really exists
onError(err.message);
}
}
}
// Using optional chaining with function calls
function doSomething(onContent, onError) {
try {
// ... do something with the data
} catch (err) {
onError?.(err.message); // no exception if onError is undefined
}
}
ííììì Optional chaining
optional chaining ì°ì°ì를 ìì±ì ííìì¼ë¡ ì ê·¼í ë ëê´í¸ í기ë²(the bracket notation of the property accessor)ì ì¬ì©í ì ìë¤:
let nestedProp = obj?.["prop" + "Name"];
Optional chainingì í ë¹ì ì¼ìª½ìì ì í¨íì§ ììµëë¤
let object = {};
object?.property = 1; // Uncaught SyntaxError: Invalid left-hand side in assignment
Optional chainingì¼ë¡ ë°°ì´ í목ì ì ê·¼í기
let arrayItem = arr?.[42];
ìì 기본 ìì
ì´ ìì ë í´ë¹ 멤ë²ê° ìì ë, mapìì ë©¤ë² barì name
ì ìì± ê°ì ì°¾ëë¤. ê·¸ë¬ë¯ë¡ ê²°ê³¼ë undefined
ì´ë¤.
let myMap = new Map();
myMap.set("foo", { name: "baz", desc: "inga" });
let nameBar = myMap.get("bar")?.name;
ë¨ë½ íê°
ííììì optional chainingì ì¬ì©í ë, ë§ì½ ì¼ìª½ì ìë í¼ì°ì°ìê° null
or undefined
ì¸ ê²½ì°, ê·¸ ííìì íê°ëì§ ìëë¤. ìë¤ ë¤ì´:
let potentiallyNullObj = null;
let x = 0;
let prop = potentiallyNullObj?.[x++];
console.log(x); // 0 xë ì¦ê°íì§ ìì
optional chaining ì°ì°ì ì기
ì¤ì²©ë 구조ììë optional chainingì ì¬ë¬ ë² ì¬ì©í ì ìë¤:
let customer = {
name: "Carl",
details: {
age: 82,
location: "Paradise Falls", // detailed address is unknown
},
};
let customerCity = customer.details?.address?.city;
// ⦠this also works with optional chaining function call
let duration = vacations.trip?.getTime?.();
ë ë³í© ì°ì°ìì ê°ì´ ì¬ì©í기
ë ë³í© ì°ì°ìë optional chaining를 ì¬ì©í íì ì무 ê°ë ì°¾ì ì ìì ë 기본 ê°ì 주기 ìí´ ì¬ì©ë ì ìë¤:
let customer = {
name: "Carl",
details: { age: 82 },
};
const customerCity = customer?.city ?? "Unknown city";
console.log(customerCity); // Unknown city
ëª
ì¸ì ë¸ë¼ì°ì í¸íì± ì°¸ê³
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4