* Array Destructuring
[x, y, ...restof] = [10, 20, 30, 40, 50];
console.log(x); // 10
console.log(y); // 20
console.log(restof); // [30, 40, 50]
* Object Destructuring
({ x, y} = { x: 10, y: 20 });
console.log(x); // 10
console.log(y); // 20
function append(value, array = []) {
array.push(value);
return array;
}
console.log(append(1)); //[1]
console.log(append(2)); //[2], not [1, 2]
* Array Literals
const array2 = [1,2,3];
const array3 = [...array2, 4, 5, ...[6]];
console.log(array3); /*[1,2,3,4,5,6]*/
var x = 333, y = 3;
var z = x > y ? "x가 y보다 더 큽니다." : "x가 y보다 더 작습니다.";
document.write(z);
const first = 'Dongchan';
const last = 'Kim';
// ES5: 문자열 연결
console.log('My name is ' + first + ' ' + last + '.');
// "My name is Dongchan Kim."
// ES6: String Interpolation
console.log(`My name is ${first} ${last}.`);
// "My name is Dongchan Kim."
let user = {}; // 주소 정보가 없는 사용자
alert( user?.address?.street ); // undefined, 에러 X
let height = 0;
alert(height || 100); // 100
alert(height ?? 100); // 0
setTimeout(function(){
console.log("2")
},1000);
// 1초 후에 콘솔에 "2"가 나타남.
① state
Heavy한 operation이 실행중인지, 완료되었는지, Error인지 판별한다.
[ pending(대기 상태) → fulfilled || rejected(실행완료 or 에러상태) ]
② producer VS consumer
const promise = new Promise(resolve(), reject()=>{
'doing something heavy...'
})
heavy한 데이터를 가져올 때, 다음 것들이 실행되지 않는다.
→ 비동기적으로 실행시켜본다.
(when new promise is created, the executor runs automatically!)
1. promise.then((value)=>{})
promise가 정상적으로 작동이 잘될 때, 해당 값이 value인자로 들어오게 된다.
- 값을 전달받아도 OK, 비동기 promise를 받아도 OK
2. promise.catch((value)=>{})
error가 나타났을 때 어떻게 처리할 것인지에 대한 함수.
3. promise.finally((value)=>{})
실패든, 에러든, 상관없이 무조건!! 마지막에 실행되는 함수.
JS 는 동기적인 처리를 하기 때문에.
Promise라는 오브젝트를 가지고 있으면, then이라는 콜백함수만 등록해놓으면, 유저의 데이터가 준비되는 대로 등록한 콜백함수를 불러온다는 느낌..?으로 이해한다.
→ 이때 producer 입장에서, resolve()나 reject를 이용해서 완료처리를 해준다.
async를 function 앞에 써주면, promise를 쓰지 않아도 된다.
→ 코드 블럭이 자동적으로 비동기 promise로 바뀐다.
promise.all(getapple(),getbanna())
비동기적으로 동시에 실행시킨다. 동기적으로 한줄씩 실행시키는 것이 아니라,
when new promise is created, the executor runs automatically를 이용해서 동시에 실행시켜 런타임을 줄이는 용도? 이다.
promise.race(getapple(),getbanna())
두개의 함수의 런타임중에서 더 먼저 수행되는 함수를 먼저 부르는 프로미스 함수이다.
이거 외에도 이번주에는 많은 양의 코딩테스트와
자바스크립트 기본 개념들을 알차게 공부했다.
map,filtet,reduce,splice,forEach등과 같은 내장함수와 친해지는 연습을 정말 많이 했고,
지금은 내장함수 없으면 코딩을 하지 못할 지경이 되어버렸다..ㅎㅋㅎㅋㅋㅋㅎㅋㅎㅋㅎ
다음주는 조금더 심화적인 자바스크립트 개념에 대해 공부할 예정인데,
벌써 신난다. 궁금하다.
꾸준히 성장하자.