์๋ฐ์คํฌ๋ฆฝํธ์์ this์ ๊ฐ์ ํจ์๊ฐ ํธ์ถ๋ ๋ ๊ฒฐ์ ๋๋ฉฐ, ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋ค๋ฅด๊ฒ ์๋ํฉ๋๋ค. ์๋์์ ์ฃผ์ ๊ฒฝ์ฐ์ ์์ ๋ฅผ ํตํด ์ค๋ช
ํ๊ฒ ์ต๋๋ค.
์ ์ญ ์ปจํ ์คํธ์์์ this
ํจ์๊ฐ ์ ์ญ ์ค์ฝํ์์ ํธ์ถ๋ ๋, this๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค. ๋ธ๋ผ์ฐ์ ์์๋ window, Node.js์์๋ global์
๋๋ค.
console.log(this); // ๋ธ๋ผ์ฐ์ ์์ ์คํ ์: Window ๊ฐ์ฒด function sayHello() { console.log(this); } sayHello(); // Window ๊ฐ์ฒด (์ ์ญ ํธ์ถ)
"use strict" ๋ชจ๋์์๋ ์ ์ญ ํธ์ถ ์ this๊ฐ undefined๋ก ์ค์ ๋ฉ๋๋ค.
"use strict"; function sayThis() { console.log(this); } sayThis(); // undefined
๊ฐ์ฒด ๋ฉ์๋์์์ this
ํธ์ถ๋๋ ํจ์๊ฐ ๊ฐ์ฒด์ ๋ฉ์๋์ผ๋, this๋ ํด๋น ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
const person = { name: "Alice", greet: function() { console.log(this.name); } }; person.greet(); // "Alice" (this๋ person ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํด)
ํ์ง๋ง ํจ์๋ฅผ ๊ฐ์ฒด์์ ๋ถ๋ฆฌํด ํธ์ถํ๋ฉด this๊ฐ ๊ฐ์ฒด๋ฅผ ์์ด๋ฒ๋ฆฝ๋๋ค.
const person = { name: "Hannah", sayName: function() { console.log(this.name); } }; const func = person.sayName; func(); // undefined (this๋ ์ ์ญ ๊ฐ์ฒด Window๋ฅผ ๊ฐ๋ฆฌํด)
๋ด๋ถ ํจ์์์์ this ๋ฌธ์
์ผ๋ฐ ํจ์ ์์ ๋ ๋ค๋ฅธ ์ผ๋ฐ ํจ์๊ฐ ์์ ๊ฒฝ์ฐ, ๋ด๋ถ ํจ์์ this๋ ์ธ๋ถ ํจ์์ this๋ฅผ ์๋์ผ๋ก ์์ํ์ง ์์ต๋๋ค.
const obj = { name: "Kate", outer: function() { console.log(this.name); //Kate function inner() { console.log(this.name); //undefined (this๋ Window) } inner(); } }; obj.outer(); // ์ถ๋ ฅ: // Outer this: Kate // Inner this: undefined (this๋ Window)
๐ ํด๊ฒฐ๋ฐฉ๋ฒ
๊ฐ์ฒด ๋ฉ์๋์์์ this๋ฅผ ๋ณ์์ ์ ์ฅํ์ฌ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ
const obj = { name: "Kate", outer: function() { const self = this; function inner() { console.log(self.name); // Inner this: Kate } inner(); } }; obj.outer(); // Inner this: Kate
๊ฐ์ฒด ๋ฉ์๋์์์ this๋ฅผ ์ฌ์ฉํ ์ ์๋ bind() ์ฌ์ฉ๋ฒ
const obj = { name: "Kate", outer: function() { const inner = function() { console.log(this.name); }.bind(this); inner(); } }; obj.outer(); // Inner this: Kate
์์ฑ์ ํจ์์์์ this
new ํค์๋๋ก ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ๋ฉด, this๋ ์๋ก ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
function Car(model) { this.model = model; } const myCar = new Car("Tesla"); console.log(myCar.model); // "Tesla"
๋ช ์์ ๋ฐ์ธ๋ฉ (call, apply, bind)
call, apply, bind๋ฅผ ์ฌ์ฉํ๋ฉด this๋ฅผ ๋ช
์์ ์ผ๋ก ์ค์ ํ ์ ์์ต๋๋ค.
๐ call ์์
function introduce(age) { console.log(this.name, age); } const user = { name: "Bob" }; introduce.call(user, 25); // "Bob, 25"
๐ apply ์์
function introduce(age) { console.log(this.name, age); } const user = { name: "Bob" }; introduce.apply(user, [30]); // "Bob 30"
ํธ์ถ ์์ ์ this๋ฅผ ๋์ ์ผ๋ก ๊ฒฐ์ ํด์ผ ํ๋ ์ํฉ์์๋ call์ด๋ apply๊ฐ ๋งค์ฐ ์ ์ฉํฉ๋๋ค.
๐ bind ์์
function introduce(age) { console.log(this.name, age); } const user = { name: "Bob" }; const boundIntroduce = introduce.bind(user); boundIntroduce(35); // "Bob 35"
๐ ํ์ดํ ํจ์์์์ this(๊ฐ์ฅ ๊ฐ๋จํ ํด๊ฒฐ์ฑ )
const obj = { name: "Charlie", sayName: function() { const arrowFunc = () => console.log(this.name); arrowFunc(); } }; obj.sayName(); // "Charlie" (this๋ obj๋ฅผ ๊ฐ๋ฆฌํด)
ํ์ดํ ํจ์๋ this๋ฅผ lexical scope(์ ์ ๋ฒ์)์์ ๊ฐ์ ธ์ต๋๋ค. ์ฆ, ํจ์๊ฐ ์ ์๋ ์์น์ ์์ ์ค์ฝํ์์ this๋ฅผ ์์๋ฐ์ต๋๋ค.
๐ ์ผ๋ฐ ํจ์(์ธ๋ถ ์ค์ฝํ์ this๋ฅผ ์ ์ฅ)
const obj2 = { name: "David", sayName: function() { const self = this; const normalFunc = function() { console.log(self.name); }; normalFunc(); } }; obj2.sayName(); // "David"
self ๋ณ์์ ์ธ๋ถ ์ค์ฝํ์ this๋ฅผ ์ ์ฅํ์ฌ ๋ด๋ถ ํจ์์์ ์ฌ์ฉํฉ๋๋ค.
์ด ์ธ ๊ฐ์ง ๋ฐฉ๋ฒ ๋ชจ๋ ๋์ผํ ๊ฒฐ๊ณผ๋ฅผ ์ ๊ณตํ๋ฉฐ, "David"๋ฅผ ์ถ๋ ฅํฉ๋๋ค. ๊ฐ์ฅ ํ๋์ ์ด๊ณ ๊ฐ๊ฒฐํ ์ ๊ทผ๋ฒ์ ์ฒซ ๋ฒ์งธ ํ์ดํ ํจ์ ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ ๊ฒ์
๋๋ค. ์ํฉ์ ๋ฐ๋ผ ์ ์ ํ ๋ฐฉ๋ฒ์ ์ ํํ ์ ์์ต๋๋ค:
- ํ์ดํ ํจ์: ๊ฐ๊ฒฐํ๊ณ ์ง๊ด์
- bind(): ๋ช ์์ ๋ฐ์ธ๋ฉ ํ์ ์
- self ๋ณ์: ์ค๋๋ ์ฝ๋์์ ํธํ์ฑ์ด๋ ํน์ ์ํฉ์์
์ด๋ฒคํธ ํธ๋ค๋ฌ์์์ this
์ด๋ฒคํธ ํธ๋ค๋ฌ๋ก ํจ์๊ฐ ํธ์ถ๋๋ฉด, this๋ ์ผ๋ฐ์ ์ผ๋ก ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ DOM ์์๋ฅผ ๊ฐ๋ฆฌํต๋๋ค.
document.querySelector("button").addEventListener("click", function() { console.log(this); // <button> ์์ });
ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ ์ด๋ฒคํธ ํธ๋ค๋ฌ์์์ this
document.querySelector("button").addEventListener("click", () => { console.log(this); // Window ๊ฐ์ฒด (์์ ์ค์ฝํ์ this) });
๊ฒฐ๊ณผ์ ์ผ๋ก
- ์ ์ญ ํธ์ถ: window (strict ๋ชจ๋์์๋ undefined).
- ๊ฐ์ฒด ๋ฉ์๋: ํธ์ถํ ๊ฐ์ฒด.
- ๋ช ์์ ๋ฐ์ธ๋ฉ: call, apply, bind๋ก ์ง์ ํ ๊ฐ์ฒด.
- ์ด๋ฒคํธ ํธ๋ค๋ฌ: ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์์.
- ์์ฑ์ ํจ์: ์๋ก ์์ฑ๋ ๊ฐ์ฒด.
- ๋ด๋ถ ํจ์: ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๊ฒฐ์ (๋ณดํต ์ ์ญ ๊ฐ์ฒด).
์ผ๋ฐ ํจ์๋ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ this๊ฐ ์ ์ฐํ๊ฒ ๋ณํ๋ค๋ ์ ์์ ํ์ดํ ํจ์์ ๋๋น๋ฉ๋๋ค. ํ์ดํ ํจ์๋ ์์ ์ค์ฝํ์ this๋ฅผ ๊ณ ์ ์ ์ผ๋ก ์ฌ์ฉํ์ง๋ง, ์ผ๋ฐ ํจ์๋ ํธ์ถ ์์ ์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ฏ๋ก ์ฃผ์๊ฐ ํ์ํฉ๋๋ค. ์ถ๊ฐ ์ง๋ฌธ์ด ์์ผ๋ฉด ๋ง์ํด์ฃผ์ธ์!