3주 차 때 했었던 고차 함수를 underscore.js라는 기술을 가지고 모티브를 얻은 Underbar 과제를 시작했다. 결론은.. 3주 차에 했었던 고차 함수는 순한 맛이었던 걸로.. underbar는 아주 매운 맛처럼 느껴진다. 내가 멍충해진것 같다 ㅠㅠ..
// global scope execution context -> 3 개 (a, func, this)
// func scope execution context -> 2 개 (b, this)
let a = 1;
function func () {
let b = 10;
}
let obj = {
a : 10;
foo : function() {console.log(this)}
}
obj.foo(); // { a: 10, foo: [Function: foo] }
```
```jsx
let obj = {
fn: function (a, b) {
return this;
}
};
let obj2 = {
method: obj.fn
}
// Point : 실행되는 시점
console.log(obj2.method() === obj2); //true
console.log(obj.fn() === obj); //true
new
연산자로 생성된 function scope의 this) : 새로 생성된 객체 // .call
let add = function (x, y) {
this.sum = x + y;
}
let obj = {
sum: 0
};
add.apply(obj, [2, 8]); // apply는 인자를 배열로 넘겨 준다.
console.log(obj.sum);
add.call(obj, 2, 8);
console.log(obj.sum);
## bind
- call/apply와 다르게 함수를 바르게 실행시키지 않는다.
- this의 값이 바인딩된 **함수 자체**를 리턴한다.
function Box(w, h) {
this.width = w;
this.height = h;
this.getArea = function () {
return this.width * this.height;
}
this.print = function () {
console.log(this.getArea());
}
}
let b = new Box(100, 50);
b.print();
// setTimeout(b.print, 1000); // TypeError
setTimeout(b.print.bind(b), 1000);