일반적으로 메소드를 호출한 객체의 정보가 저장되어 있는 속성.
console.log(this)
// Window {0: global, 1: global, 2: global, window: Window, …}
function outer() {
console.log(this);
}
outer();
// Window {0: global, 1: global, 2: global, window: Window, …}
function outer() {
function inner() {
console.log(this);
}
inner();
}
outer();
// Window {0: global, 1: global, 2: global, window: Window, …}
const foo = {
name: 'bear',
honey: function() {
console.log(this.name, this);
}
}
foo.honey();
// bear {name: 'bear', honey: ƒ}
function one(callback) {
console.log(callback())
}
function two() {
console.log(this)
}
one(two)
// Window {window: Window, self: Window, …}
function User(name) {
this.name = name;
this.speak = function() {
console.log('hello! '+this.name, this);
}
}
const getChris = new User('chris');
const getAnne = new User('anne');
getChris.speak();
getAnne.speak();
// hello! chris User {name: 'chris', speak: ƒ}
// hello! anne User {name: 'anne', speak: ƒ}