var
let
const
백틱으로 감싼 문자열에 ${myVar}
활용하는 것. 웹에서 많이 쓰쥬?
function sayNode() {
console.log('Node');
}
const newObject = {
sayJS() {
console.log('JS');
}, // adding function as attr easier
sayNode, // adding attr concisely
[es + 6]: 'Fantastic', // attribute created dynamically
};
여기서는 this에 대해서만 정리한다, 나중에도 학습하고 꼭 기억할 것.
객체의 구조분해 할당
const example = { a:123, b: {c : 134, d:146}};
const a = example.a;
const b = example.b.d;
const {a, b: {d}} =example;
console.log(a); // 123
console.log(d); //146
배열의 구조분해 할당
const arr = [1,2,3,4,5]
const [a,b,,,d] = arr;
console.log(a); // 1
console.log(d); // 5
모듈 require에서 구조분해 할당
module.exports = {odd, even} // var.js
const {odd, even} = require('./var'); // main.js
프로토타입을 깔끔하게 작성할 수 있는 Class 문법이 추가되었음
일반적인 클래스 생성문법을 활용가능하다, 추후 필요시 구글링~
노드에서 주로 구현되는 비동기 특히, 이벤트 리스너 구현시 발생하는 콜백지옥에서 벗어나기 위해 도입된 개념
promise와 .then .catch .finally
아래의 코드처럼 .then 체이닝을 위해서 선행.then에선 promise를 리턴해야 함
promise
.then(message => {
return new Promise((resolve, reject) => {
resolve(message);
});
})
.then(message2 => {
console.log(message2);
})
.catch(error => {
console.log(error);
});
const promise = new Promise(...);
async function main() { // async에서 오류 처리를 위해 try..catch
try {
const result = await promise;
return result;
} catch (err) {
console.log(err);
}
}
main().then(name => {...}); // main 함수의 리턴값을 콜백함수에서 접근가능
const name = await main(); // main 함수의 리턴값이 name에 저장됨
const result = await axios.get(`https://www.zerocho.com/${encodeURIComponent('노드')}`);