기본적으로 this는 window다.
function a() {
console.log(this); // window
}
a();
object의 경우
var obj = {
a: function() {
console.log(this);
},
b: =>
}
obj.a(); // obj
var a2 = obj.a;
a2(); // window
arrow function을 사용했을때
var obj = {
a: () => {
console.log(this); // window
}
}
.bind()
.call()
.apply()
var obj2 = { c: 'd' };
function b() {
console.log(this);
}
b(); // Window
b.bind(obj2).call(); // obj2
b.call(obj2); // obj2
b.apply(obj2); // obj2
function Person(name, age) {
this.name = name;
this.age = age;
}
Person.prototype.sayHi = function() {
console.log(this.name, this.age);
}
new 없이 호출하는 경우
Person('ZeroCho', 25);
console.log(window.name, window.age); // ZeroCho 25