const add = (a, b) => a + b;
function calculator(func, a, b) {
return func(a, b);
}
add(3, 5); // 8
calculator(add, 3, 5); // 8
calculator(add(), 3, 5) // undefined + undefined
스코프 체인이란 함수에서 어떤 값에 접근이 가능하냐, 불가능하야
블록이라는 것을 기준으로 나뉨
const obj = {
name: 'him',
sayName() {
console.log(this.name)
}
}
obj.sayName()
-> him
window.name
-> ''
const sayN = obj.sayName();
sayN()
-> ''
bind()
apply()
call()
function sayName(){
console.log(this.name);
} -> undefined
sayName.bind({name: 'him'})() // 호출을 따로 해야함.
-> him
sayName.apply({name: 'him'}) // 호출을 해줌 매개변수가 배열
-> him
sayName.call({name: 'him'})() // 호출을 해줌 매개변수가 순서대로
-> him
const obj = {
name: = 'him',
sayName() {
console.log(this.name);
const inner = () => {
console.log(this.name);
}
inner();
}
}
obj.sayName(); // this = obj;
-> him
-> him
const sayN = obj.sayName; // this = window
sayN();
-> ''
-> ''
this는 함수의 호출 시점에 따라 정해짐
화살표 함수는 부모의 this를 그대로 사용.
function 은 this가 window를 가르켜서 name은 '' 이 됨.
const p1 = axios.post('서버주소1')
const p2 = axios.post('서버주소2')
const p3 = axios.post('서버주소3')
const p4 = axios.post('서버주소4')
const p5 = axios.post('서버주소5')
const p6 = axios.post('서버주소6')
promise.all([p1,p2,p3,p4,p5,p6])then.(results) => {}).catch((error) => {}); // 어떤게 실패했는지 알 수 없음
promise.allsettled([p1,p2,p3,p4,p5,p6]).then((results) => {}).catch((error) => {}); // 어떤게 실패했는지 다시 시도 가능
.finally(() => {}); // 성공하든 실패하든 실행되는 코드