Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | 5 | 6 | |
| 7 | 8 | 9 | 10 | 11 | 12 | 13 |
| 14 | 15 | 16 | 17 | 18 | 19 | 20 |
| 21 | 22 | 23 | 24 | 25 | 26 | 27 |
| 28 | 29 | 30 | 31 |
Tags
- Default parameter
- Python #Baekjoon
- 카카오맵 api
- Hooks
- HTML
- spread operation
- 프로그래머스
- css #html
- optional chanining
- react
- CSS
- Template literals
- nextjs
- Redux
- Nullish Coalescing Operator
- Python
- es11
- firebase
- Python #CodeUp
- Next
- JavaScript
- BOJ
- es6
- HTML #CSS
- React #Hooks
- React Kakao map
- 카카오맵
Archives
- Today
- Total
거북이개발자
[ES11] Optional chaining 본문
1. Optional chaining
const person1={
name:'Ryu'
job : {
title 'Engineer',
manager:{
name : bob,
},
},
};
const person2={
name:'Kim',
};
{
function printManager(person){
console.log(person.job.manager.name);
}
}
person1 과 person2의 객체가 있고, 매니저의 이름을 프런트하는 함수를 구현하고 싶다.
(1)문제점
printManager(person1);

printManager(person2);

위의 사진처럼 person2객체는 job, manger이 정의 되어있지 않아서 오류가 난다.
(2)해결(&&연산자)
{
function printManager(person){
console.log(person.job && preson.job.manager && person.job.manager.name);
}
}
위처럼 && 연산자를 이용할 수 도 있다.
(3)해결(Optional chaining)
{
function printManager(person){
console.log(person.job?.manager?.name);
}
}
optional chaining을 이용하면 더욱 간소하게 표현할 수 있다.
'Web > ES6' 카테고리의 다른 글
| [ES11] Nullish Coalescing Operator (0) | 2021.02.07 |
|---|---|
| [ES6] Template Literals (0) | 2021.02.07 |
| [ES6] Default parameters (0) | 2021.02.07 |
| [ES6] Spread Syntax (0) | 2021.02.07 |
| [ES6] Destructuring Assignment (0) | 2021.02.07 |
Comments