<aside> 💡 함수에서 this가 다른것을 가리키게 하고 싶을 때 bind, call, apply 사용
</aside>
bind함수가 call, apply와 다른 점은 함수를 실행하지 않는다는 점이다. call과 apply는 함수를 호출하는 함수이다. 그러나 그냥 실행하는 것이 아니라 첫 번째 인자에 this로 setting하고 싶은 객체를 넘겨주어 this를 바꾸고나서 실행한다. call과 apply의 유일한 차이점은, 첫 번째 인자(this를 대체할 값)를 제외하고, 실제 say에 필요한 parameter를 입력하는 방식이다. call과는 다르게 apply함수는 두 번째 인자부터 모두 배열에 넣어야 한다.
<aside> 💡 console.log는 동기 작업, setTimeout은 비동기 작업이므로 webapi에서 처리한다. setTimeout의 시간이 지나서 완료되면 콜백 큐로 함수가 들어오게 되고, web api의 콜백함수들이 콜백큐에서 대기하게 된다. 예를 들면, 11번째 콜스택 호출
</aside>
* 콜스택이 비게 되면 먼저 들어온 순서대로 콜백 큐에 함수를 넣게 된다.
function B() {
setTimeout(function () {
console.log("B-1...");
}, 1500);
}
function A() {
console.log("A-1...");
B();
console.log("A-2...");
}
A();
{
name: "mike smith",
family: {
mother: "jane smith",
father: "harry smith",
sister: "samantha smith"
},
age: 35
},
{
name: 'tom jones',
family: {
mother: "norah jones",
father: "richard jones",
brother: "howard jones"
},
age: 25
}
];
for (var {name: n, family: { father: f} } of people) {
console.log("Name: " + n + ", Father: " + f);
}
결과
> "Name: mike smith, Father: harry smith"
> "Name: tom jones, Father: richard jones"
<aside> 💡 전개 연산자에서 기존배열을 보존하고 싶을 때는?
</aside>
const arr1 = [1,2,3];
const arr2 = [...arr1].reverse();
//새로운 것을 reverse한것이므로 arr1의 불변성은 유지가된다.
<aside> 💡 map() 메서드: 새로운 배열을 반환 arr.map(callBack(currentValue[, index[,array]]),[,thisArg]) filter() 메서드: reduce() 메서드:
</aside>
<aside> 💡 iife 의 사용 목적은 변수를 전역으로 선언하는 것을 피하고, 내부안으로 다른 변수들이 접근하는 것을 막기 위해서이다.
</aside>
iife 폴더 생성 - script.js
index.html 안녕하세요 코드
##
let x = 0;
const numberUp = () => x += 1;
console.log(numberUp());
console.log(x);
let x = 0;
const pureNumberUp = (num) => num += 1;
console.log(pureNumberUp(x));
console.log(x);