
...
function playAudio() {
console.log(this);
}
playAudio();
const fullName = function () {
console.log(this.firstName + " " + this.lastName);
}
const person1 = {
firstName: "John",
lastName: "Doe"
}
fullName.call(person1);

원래 함수에서
this는 Window 가리켜서 둘 다undefined출력 되지만
이this가 Window가 아닌person1이 생성한 객체 가리키게 하기 위해fullName.call메소드 사용해서person1을 인수로 넣어 호출하면
이제 함수 안this는 Window가 아닌person1객체 가리키게 됨😮



const fullName = function (city, country) {
console.log(this.firstName + " " + this.lastName, city, country);
}
const person1 = {
firstName: "John",
lastName: "Doe"
}
// 인수 [] 배열로 넣어주기
fullName.apply(person1, ["Oslo", "Norway"]);
function fullName(city, country) {
console.log(this.firstName + " " + this.lastName, city, country);
}
const person1 = {
firstName: "John",
lastName: "Doe"
};
const args = ["Oslo", "Norway"];
fullName.call(person1, ...args);
// 결과: John Doe Oslo Norway
function func(language) {
if(language === 'kor') {
console.log(`language: ${this.korGreeting}`);
} else {
console.log(`language: ${this.engGreeting}`);
}
}
func('kor')
function func(language) {
if(language === 'kor') {
console.log(`language: ${this.korGreeting}`);
} else {
console.log(`language: ${this.engGreeting}`);
}
}
const greeting = {
korGreeting: "안녕 ",
engGreeting: "Hello "
}
// this가 greeting 객체를 참조하도록
// 바인딩이 된 함수가 반환이 된 것-> boundFunc
const boundFunc = func.bind(greeting);
boundFunc('kor');
