Optional Chaining์ ?.
์ฐ์ฐ์์ด๋ค. ์๋ ์์๋ฅผ ์ดํด๋ณด์.
1
2
3
4
5
6
7
8
9
| const user = {
/*
name: {
first: "Minsoo",
last: "Kim"
},
*/
age: 15
}
|
์ค๋ฅ์์ด ์ ์์ด๋ค.
1
2
| > user.name
undefined
|
1๊ฐ์ ๊ฐ์ฒด์ ๋ํ์ฌ ์ ๊ทผํ ๋๋ undefined๋ฅผ ๋ฐํํ๋ค.
1
2
| > user.name.first
Uncaught TypeError: Cannot read properties of undefined
|
์์ ๊ฐ์ด 2๊ฐ ์ด์์ ๊ฐ์ฒด์ ๋ํ์ฌ ์ ๊ทผํ ๋๋ TypeError๋ฅผ ๋ฐํํ๋ค.
์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ์คํฌ๋ฆฝํธ๋ ๋ ์ด์ ์งํํ์ง ์๊ธฐ ๋๋ฌธ์ ?. ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ ์๋ฌ ์์ด ์์ ํ๊ฒ ์ ๊ทผํ ์ ์๋ค.
1
2
| > user.name?.first
undefined
|
Leave a comment