
ใthisBinding์ ์คํ ์ปจํ
์คํธ๊ฐ ํ์ฑํ๋ ๋ ์ด๋ฃจ์ด์ง๋ค. ์คํ ์ปจํ
์คํธ๋ ํจ์๊ฐ ํธ์ถ๋๋ ์๊ฐ์ ์์ฑ๋๋ฉฐ, ์ด ๋ this๊ฐ ๊ฒฐ์ ๋๋ค. ๋ฐ๋ผ์ ์ฝ๋๋ก๋ this๊ฐ ์ด๋ค ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํฌ์ง ๋ฏธ๋ฆฌ ์์ธกํ ์ ์๋ค.
ใthisBinding์ JavaScript์์ ์คํ ์ปจํ
์คํธ์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋์ง๋ง, ๋ช
์์ ์ผ๋ก ๋ฐ์ธ๋ฉํ์ฌ ํด๋น ์ปจํ
์คํธ์์์ this ๊ฐ์ ํน์ ๊ฐ์ฒด๋ก ๊ณ ์ ํ ์ ์๋ค. ์ด๋ฅผ ํตํด this์ ํด๋น ๊ฐ์ฒด๋ฅผ ์ฐ๊ฒฐ์ํฌ ์ ์๋ค. ์ด๋ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋๋๋์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ๋์ํ๋ฉฐ, ์ด๋ฌํ ๋์์ ๋์ ๋ฐ์ธ๋ฉ์ด๋ผ๊ณ ํ๋ค.
ใ์ ์ญ ๊ณต๊ฐ์์์ this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค. ์ ์ญ ์ปจํ
์คํธ๋ฅผ ์คํํ๋ ์ฃผ์ฒด๊ฐ ์ ์ญ ๊ฐ์ฒด์ด๊ธฐ ๋๋ฌธ์ด๋ค. ์ด๋ฌํ ์ ์ญ ๊ฐ์ฒด๋ ํธ์คํธ ํ๊ฒฝ์ ๋ฐ๋ผ ๋ค๋ฅด๋ฉฐ, ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์๋ window ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๊ณ , Node.js ํ๊ฒฝ์์๋ global ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ค.
ใ์ ์ญ ๊ฐ์ฒด๋ ์ ์ญ ๋ฒ์์ ํญ์ ์กด์ฌํ๋ ๊ฐ์ฒด๋ก, ์ ์ญ ๋ณ์ ๋ฐ ์ ์ญ ํจ์๋ฅผ ํฌํจํ์ฌ ์ ์ญ ํ๊ฒฝ์์ ์ฌ์ฉ๋๋ ๋ชจ๋ ๊ฐ์ฒด์ ํจ์๊ฐ ์ด ๊ฐ์ฒด์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ก ์กด์ฌํ๋ค. ๋ฐ๋ผ์ ์ ์ญ ๋ฒ์์์ ์คํ๋๋ ์ฝ๋์์ this๋ฅผ ์ฌ์ฉํ๋ฉด ํด๋น ์ฝ๋๊ฐ ์คํ๋๋ ์ปจํ
์คํธ์ ๋ฐ๋ผ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
console.log(this === window); // ๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ true
console.log(this === global); // Node.js ํ๊ฒฝ์์ true
์ฒซ ํ์ ์ ์ญ ๊ฐ์ฒด์ธ window ๊ฐ์ฒด์ this๊ฐ ๋์ผํ๋ฏ๋ก true๋ฅผ ์ถ๋ ฅํ๊ณ , ๋ ๋ฒ์งธ ํ์ global ๊ฐ์ฒด์ this๊ฐ ๋์ผํ๋ฏ๋ก true๋ฅผ ์ถ๋ ฅํ๋ค.
ใ์ด๋ค ํจ์๋ ์์ ํ์ง ์์ ํจ์๊ฐ ์ ์ญ ๋ฒ์์์ ํธ์ถ๋ ๋, JavaScript ์์ง์ this๋ฅผ ์ ์ญ ๊ฐ์ฒด๋ก ์ค์ ํ๋ค. ์ด๊ฒ์ "์ ์ญ ํธ์ถ"์ด๋ผ๊ณ ๋ ํ๋ค. ์ ์ญ ๋ฒ์์์ ํจ์๊ฐ ํธ์ถ๋๋ ๊ฒฝ์ฐ์๋ this๊ฐ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๊ฒ ๋๋ค.
ใES6๋ถํฐ ๋์
๋ ํ์ดํ ํจ์(arrow function)๋ ํจ์๋ฅผ ์ ์ํ ๋ ์์ฒด์ ์ธ this๋ฅผ ์์ฑํ์ง ์๊ณ , ํจ์๋ฅผ ๊ฐ์ธ๊ณ ์๋ ๊ฐ์ฅ ๊ฐ๊น์ด ์์ ํจ์์ this๋ฅผ ์์ํ๊ฒ ๋๋ค. ๋ฐ๋ผ์ ํ์ดํ ํจ์๋ ์์ ์ปจํ
์คํธ์ ์๋ this๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๊ฒ ๋๋ค. ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ผ๋ฐ ํจ์์ thisBinding๊ณผ ๊ด๋ จ๋ ์ผ๋ฐํ ๋ฌธ์ ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค. ๋ฉ์๋๋ ์ฝ๋ฐฑ ํจ์๋ฅผ ์ ์ํ ๋ ์ ์ฉํ๋ฉฐ, ๋ฉ์๋ ๋ด๋ถ์์ this๊ฐ ์์ ์ ๊ฐ๋ฆฌํค๋๋ก ํ๊ณ ์ถ์ ๋ ์ฌ์ฉ๋ ์ ์๋ค. ํ์ง๋ง ํ์ดํ ํจ์๋ ์์ฒด์ ์ธ this๋ฅผ ๊ฐ์ง์ง ์๊ธฐ ๋๋ฌธ์ ๋ฉ์๋ ๋ด๋ถ์์ ์ฌ์ฉํ ๋๋ ์ ํฉํ์ง ์์ ์ ์๋ค.
// ์ผ๋ฐ ํจ์
function myFunction() {
console.log(this);
}
myFunction(); // this๋ ์ ์ญ ๊ฐ์ฒด(window)
// arrow ํจ์
const myObject = {
myMethod() {
const myArrowFunction = () => {
console.log(this);
};
myArrowFunction();
}
};
myObject.myMethod(); // this๋ myMethod๊ฐ ์ํ ๊ฐ์ฒด (=myObject)
ใmethod ํธ์ถ ์ ๊ฐ์ฒด์ ๋ฉ์๋ ๋ด์์ this๋ ํด๋น ๋ฉ์๋๊ฐ ์ํ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ๋ค. ๋ฉ์๋ ํธ์ถ ์ ๋ฉ์๋๋ช
์์ ์๋ ๊ฐ์ฒด๊ฐ this๊ฐ ๋๋ค. ์ ํ๊ธฐ๋ฒ ๋๋ ๋๊ดํธ ํ๊ธฐ๋ฒ์์ ์์ ์๋ ๊ฐ์ฒด๊ฐ this๋ก ์ฌ์ฉ๋๋ค.
const myObject = {
myMethod() {
console.log(this);
}
};
myObject.myMethod(); // this๋ ๋ฉ์๋๊ฐ ์ํ ๊ฐ์ฒด (=myObject)
๋ฉ์๋๋ ๊ฐ์ฒด์ ๊ด๋ จ๋ ๋์์ ์ํํ๋ ํจ์๋ฅผ ์๋ฏธํ๋ค. ํจ์๊ฐ ์ด๋ค ๊ฐ์ฒด์ ๊ด๋ จ๋ ๋์์ ํ๋ฉด ํด๋น ํจ์๋ ๋ฉ์๋๋ก ๊ฐ์ฃผ๋๋ค. ์ด๋ ๊ฒ ๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ ๊ฐ์ฒด์ ๋์์ ์ ์ํ๊ณ ๊ตฌํํ ์ ์๋ค.
ใ๊ธฐ๋ณธ์ ์ผ๋ก ์ฝ๋ฐฑ ํจ์๋ ํธ์ถํ๋ ํจ์์ this์ ๋์ผํ๋ค. (ํด๋น ํจ์๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด) ํ์ง๋ง ์ ์ด๊ถ์ ๊ฐ์ง ํจ์๊ฐ ์ฝ๋ฐฑ์ this๋ฅผ ์ง์ ํด๋ ๊ฒฝ์ฐ, ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์์์ this๋ ํด๋น ํจ์์์ ์ง์ ๋ ๋์์ ๊ฐ๋ฆฌํจ๋ค. ๋ช
์์ ์ผ๋ก this๋ฅผ ๋ฐ์ธ๋ฉํ์ฌ ์ํ๋ ๊ฐ์ฒด๋ฅผ this๋ก ์ค์ ํ ์ ์๋ค.
*๋ช
์์ ์ผ๋ก this๋ฅผ ๋ฐ์ธ๋ฉ ํ๋ ๋ฐฉ๋ฒ : call(), apply(), bind() ๋ฉ์๋ ์ฌ์ฉ
call ๋ฉ์๋๋ ํจ์๋ฅผ ํธ์ถํ๋ฉด์ ํน์ ๊ฐ์ฒด๋ฅผ ํจ์ ๋ด๋ถ์ this๋ก ์ง์ ํ๋ค. ์ธ์๋ก ๋๊ธด ์ฒซ ๋ฒ์งธ ๊ฐ์ด ํจ์ ๋ด๋ถ์์์ this๋ก ์ค์ ๋๋ค.
const myObject = { value: 123 };
function myFunction() {
console.log(this.value);
}
myFunction.call(myObject); // 123
apply ๋ฉ์๋๋ call๊ณผ ๋น์ทํ์ง๋ง, ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ๋๋ค. ๋ฐฐ์ด์ ์ฒซ ๋ฒ์งธ ๊ฐ์ด ํจ์ ๋ด๋ถ์ this๋ก ์ค์ ๋๋ค.
const myObject = { value: 123 };
function myFunction() {
console.log(this.value);
}
myFunction.apply(myObject); // 123
bind ๋ฉ์๋๋ ํจ์๋ฅผ ํธ์ถํ์ง๋ ์์ง๋ง, ํจ์๋ฅผ ํธ์ถํ ๋ ์ฌ์ฉํ this ๊ฐ์ ์๊ตฌ์ ์ผ๋ก ๋ฐ์ธ๋ฉํ๋ค. ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ฉฐ, ์๋ณธ ํจ์์ ๋์ผํ ์ฝ๋๋ฅผ ๊ฐ์ง์ง๋ง this ๊ฐ์ด ๊ณ ์ ๋๋ค.
const myObject = { value: 123 };
function myFunction() {
console.log(this.value);
}
const result = myFunction.bind(myObject);
result(); // 123
ใ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ ๋๋ new ํค์๋๋ฅผ ์ฌ์ฉํ์ฌ ์๋ก์ด ๊ฐ์ฒด๋ฅผ ์์ฑํ๊ณ , ์ด ์๋ก์ด ๊ฐ์ฒด๊ฐ ์์ฑ์ ํจ์ ๋ด๋ถ์์์ this๊ฐ ๋๋ค.
function Person(name, age) {
this.name = name;
this.age = age;
}
const person1 = new Person('cho', 30);
const person2 = new Person('megu', 26);
console.log(person1.name); // 'cho'
console.log(person2.age); // 26
์ฝ์ด ์๋ฐ์คํฌ๋ฆฝํธ 3๊ฐ this