javascript - this
this 5 patterns
1. Global Reference
- this === window
log(this); // this === window
2. Free Function Invocation
- this === window
var fun = function() {
log(this);
}
fun(); // this === window
3. .call or .apply Invocation
- this === call / apply의 첫 번째 인자로 명시된 객체
var fun = function() {
log(this);
}
fun.call(obj); // this === obj
fun.apply(obj); // this === obj
4. Construction Mode
- this === 새로 생성된 객체
var fun = function() {
log(this);
}
new fun();
5. Method Invocation
- this === 부모 object
var obj = {method: fun};
obj.mtd(); // this === obj