- 일반 함수의 this는 호출 위치에서 정의
- 화살표 함수의 this는 자신이 선언된 함수(렉시컬: 함수가 동작할 수 있는 유효한 범위) 범위에서 정의
function user() {
this.firstName = 'Lisa'
this.lastName = 'Oh'
return {
firstName: 'Ju',
lastName: 'Oh',
age: 93,
getFullName: function () {
return `${this.firstName} ${this.lastName}`
}
}
}
const u = user();
console.log(u.getFullName());
function user() {
this.firstName = 'Lisa'
this.lastName = 'Oh'
return {
firstName: 'Ju',
lastName: 'Oh',
age: 93,
getFullName () {
return `${this.firstName} ${this.lastName}`
}
}
}
const kevin = {
fistName: 'Kevin',
lastName: 'Kim'
}
const u = user();
console.log(u.getFullName);
console.log(u.getFullName.call(kevin));
const timer = {
title: 'TIMER!',
timeout() {
console.log(this.title);
setTimeout(function () {
console.log(this.title)
}, 1000)
}
}
timer.timeout();
function user() {
this.firstName = 'Lisa'
this.lastName = 'Oh'
return {
firstName: 'Ju',
lastName: 'Oh',
age: 93,
getFullName: () => {
return `${this.firstName} ${this.lastName}`
}
}
}
const u = user();
console.log(u.getFullName());
const timer = {
title: 'TIMER!',
timeout() {
console.log(this.title);
setTimeout(() => {
console.log(this.title);
}, 1000);
}
}
timer.timeout();