const audio = {
title: "a",
play() {
console.log("play this", this);
},
};
audio.play();
audio.stop = function () {
console.log("stop this", this);
};
audio.stop();
```();
위 코드를 console.log에 출력한 결과 값
function playAuido() {
console.log(this);
}
playAuido();
위 코드를 console.log에 출력한 결과 값
function Audio2(title) {
this.title = title;
console.log(this);
}
const audio2 = new Audio2("a");
위 코드를 console.log에 출력한 결과 값
const test = {
title: "test",
categories: ["kim", "yun", "park"],
testCategories() {
// forEach의 첫 번째 매개변수: 콜백함수, 두 번째: thisArg
this.categories.forEach(
function (category) {
console.log(`title: ${this.title}, category: ${category}`);
// 함수안에 있는 this -> window object를 가르킨다.
// window object안에 title이라는 속성이 없이 때문에 undefined가 나옴
},
{ title: "test" } // 2번째 매개변수에 작성하면 콜백함수에서 this로 참조 됨
);
},
};
test.testCategories();
위 코드를 console.log에 출력한 결과 값
const test2 = {
title: "test",
categories: ["kim", "yun", "park"],
testCategories() {
this.categories.forEach((category) => {
console.log(this);
});
},
};
test2.testCategories();
위 코드를 console.log에 출력한 결과 값