๋์์ ๋ํ๋ด๋ ๋ฉ์๋๋ ์์ ์ด ์ํ ๊ฐ์ฒด์ ์ํ, ์ฆ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๊ณ ๋ณ๊ฒฝํ ์ ์์ด์ผ ํ๋ค. ์ด๋ ๋ฉ์๋๊ฐ ์์ ์ด ์ํ ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๋ ค๋ฉด ๋จผ์ ์์ ์ด ์ํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ ์๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์์ด์ผ ํ๋ค.
์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ํน์ํ ์๋ณ์๊ฐ ํ์ํ๋ค. ์ด๋ฅผ ์ํด ์๋ฐ์คํฌ๋ฆฝํธ๋ this๋ผ๋ ํน์ํ ์๋ณ์๋ฅผ ์ ๊ณตํ๋ค.
this๋ ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํค๋ ์ฐธ์กฐ ๋ณ์๋ค. this๋ฅผ ํตํด ์์ ์ด ์ํ ๊ฐ์ฒด ๋๋ ์์ ์ด ์์ฑํ ์ธ์คํด์ค์ ํ๋กํผํฐ๋ ๋ฉ์๋๋ฅผ ์ฐธ์กฐํ ์ ์๋ค.
this๊ฐ ๊ฐ๋ฆฌํค๋ ๊ฐ, ์ฆ this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์์ ์ํด ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
โ๏ธ this ๋ฐ์ธ๋ฉ
๋ฐ์ธ๋ฉ์ด๋ ์๋ณ์์ ๊ฐ์ ์ฐ๊ฒฐํ๋ ๊ณผ์ ์ ์๋ฏธํ๋ค. ์๋ฅผ ๋ค์ด ๋ณ์ ์ ์ธ์ ๋ณ์ ์ด๋ฆ๊ณผ ํ๋ณด๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ์ฃผ์๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๊ฒ์ด๋ค. this ๋ฐ์ธ๋ฉ์ this์ this๊ฐ ๊ฐ๋ฆฌํฌ ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉํ๋ ๊ฒ์ด๋ค.
// ๊ฐ์ฒด ๋ฆฌํฐ๋ด
const circle = {
radius: 5,
getDiameter(){
// this๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
return 2 * this.radius;
}
};
console.log(circle.getDiameter()); // 10
์๋ฐ์คํฌ๋ฆฝํธ์ this๋ ํจ์๊ฐ ํธ์ถ๋๋ ๋ฐฉ์์ ๋ฐ๋ผ this์ ๋ฐ์ธ๋ฉ๋ ๊ฐ, ์ฆ this ๋ฐ์ธ๋ฉ์ด ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ๋ฐฉ์, ์ฆ ํจ์๊ฐ ์ด๋ป๊ฒ ํธ์ถ๋์๋์ง์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋๋ค.
โ๏ธ ๋ ์์ปฌ ์ค์ฝํ์ this ๋ฐ์ธ๋ฉ์ ๊ฒฐ์ ์๊ธฐ๊ฐ ๋ค๋ฅด๋ค.
ํจ์์ ์์ ์ค์ฝํ๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐฉ์์ธ ๋ ์์ปฌ ์ค์ฝํ๋ ํจ์ ์ ์๊ฐ ํ๊ฐ๋์ด ํจ์ ๊ฐ์ฒด๊ฐ ์์ฑ๋๋ ์์ ์ ์์ ์ค์ฝํ๋ฅผ ๊ฒฐ์ ํ๋ค. ํ์ง๋ง this ๋ฐ์ธ๋ฉ์ ํจ์ ํธ์ถ ์์ ์ ๊ฒฐ์ ๋๋ค.
ํจ์๋ฅผ ํธ์ถํ๋ ๋ฐฉ์
๊ธฐ๋ณธ์ ์ผ๋ก this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
function foo() {
console.log("foo's this: ", this); // window
function bar() {
console.log("bar's this: ", this); // window
}
bar();
}
foo();
์ ์ญ ํจ์๋ ๋ฌผ๋ก ์ด๊ณ ์ค์ฒฉ ํจ์๋ฅผ ์ผ๋ฐ ํจ์๋ก ํธ์ถํ๋ฉด ํจ์ ๋ด๋ถ์ this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ ๋ชจ๋ ํจ์(์ค์ฒฉ ํจ์, ์ฝ๋ฐฑ ํจ์ ํฌํจ) ๋ด๋ถ์ this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
๋ฉ์๋ ๋ด๋ถ์ this์๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด, ์ฆ ๋ฉ์๋๋ฅผ ํธ์ถํ ๋ ๋ฉ์๋ ์ด๋ฆ ์์ ๋ง์นจํ(.) ์ฐ์ฐ์ ์์ ๊ธฐ์ ํ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
const person = {
name: 'Lee',
getName() {
// ๋ฉ์๋ ๋ด๋ถ์ this๋ ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด์ ๋ฐ์ธ๋ฉ๋๋ค.
return this.name;
}
};
// ๋ฉ์๋ getName์ ํธ์ถํ ๊ฐ์ฒด๋ person์ด๋ค.
console.log(person.getName()); // Lee
์์ฑ์ ํจ์ ๋ด๋ถ์ this์๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
// ์์ฑ์ ํจ์
function Circle(radius) {
// ์์ฑ์ ํจ์ ๋ด๋ถ์ this๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
this.radius = radius;
this.getDiameter = function () {
return 2 * this.radius;
};
}
// ๋ฐ์ง๋ฆ์ด 5์ธ Circle ๊ฐ์ฒด๋ฅผ ์์ฑ
const circle1 = new Circle(5);
// ๋ฐ์ง๋ฆ์ด 10์ธ Circle ๊ฐ์ฒด๋ฅผ ์์ฑ
const circle2 = new Circle(10);
console.log(circle1.getDiameter()); // 10
console.log(circle2.getDiameter())
Function.prototype.apply, Function.prototype.call ๋ฉ์๋๋ this๋ก ์ฌ์ฉํ ๊ฐ์ฒด์ ์ธ์ ๋ฆฌ์คํธ๋ฅผ ์ธ์๋ก ์ ๋ฌ๋ฐ์ ํจ์๋ฅผ ํธ์ถ
function getThisBinding() {
return this;
}
// this๋ก ์ฌ์ฉํ ๊ฐ์ฒด
const thisArg = { a:1 };
console.log(getThisBinding()); // window
// getThisBinding ํจ์๋ฅผ ํธ์ถํ๋ฉด์ ์ธ์๋ก ์ ๋ฌํ ๊ฐ์ฒด๋ฅผ getThisBinding ํจ์์ this์ ๋ฐ์ธ๋ฉํ๋ค.
console.log(getThisBinding.apply(thisArg)); // {a:1}
console.log(getThisBinding.call(thisArg)); // {a:1}
apply์ call ๋ฉ์๋์ ๋ณธ์ง์ ์ธ ๊ธฐ๋ฅ์ ํจ์๋ฅผ ํธ์ถํ๋ ๊ฒ์ด๋ค.
Function.prototype.bind ๋ฉ์๋๋ apply์ call ๋ฉ์๋์ ๋ฌ๋ฆฌ ํจ์๋ฅผ ํธ์ถํ์ง ์๋๋ค. ๋ค๋ง ์ฒซ ๋ฒ์จฐ ์๋ก ์ ๋ฌํ ๊ฐ์ผ๋ก this ๋ฐ์ธ๋ฉ์ด ๊ต์ฒด๋ ํจ์๋ฅผ ์๋กญ๊ฒ ์์ฑํด ๋ฐํํ๋ค.
function getThisBinding() {
return this;
}
// this๋ก ์ฌ์ฉํ ๊ฐ์ฒด
const thisArg = { a: 1};
// bind ๋ฉ์๋๋ ์ฒซ ๋ฒ์จฐ ์ธ์๋ก ์ ๋ฌํ thisArg๋ก this ๋ฐ์ธ๋ฉ์ด ๊ต์ฒด๋
// getThisBinding ํจ์๋ฅผ ์๋กญ๊ฒ ์์ฑํด ๋ฐํํ๋ค.
console.log(getThisBinding.bing(thisArg)); // getThisBinding
// bind ๋ฉ์๋๋ ํจ์๋ฅผ ํธ์ถํ์ง๋ ์์ผ๋ฏ๋ก ๋ช
์์ ์ผ๋ก ํธ์ถํด์ผ ํ๋ค.
console.log(getThisBinding.bind(thisArg)()); {a:1}
bind ๋ฉ์๋๋ ๋ฉ์๋์ this์ ๋ฉ์๋ ๋ด๋ถ์ ์ค์ฒฉ ํจ์ ๋๋ ์ฝ๋ฐฑ ํจ์์ this๊ฐ ๋ถ์ผ์นํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ์ ์ฉํ๊ฒ ์ฌ์ฉ
ํจ์ ํธ์ถ ๋ฐฉ์ | this ๋ฐ์ธ๋ฉ |
---|---|
์ผ๋ฐ ํจ์ ํธ์ถ | ์ ์ญ ๊ฐ์ฒด |
๋ฉ์๋ ํธ์ถ | ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด |
์์ฑ์ ํจ์ ํธ์ถ | ์์ฑ์ ํจ์๊ฐ (๋ฏธ๋์) ์์ฑํ ์ธ์คํด์ค |
Function.prototype.apply/call/bind ๋ฉ์๋์ ์ํ ๊ฐ์ ํธ์ถ | Function.prototype.apply/call/bind ๋ฉ์๋์ ์ฒซ๋ฒ์จฐ ์ธ์๋ก ์ ๋ฌํ ๊ฐ์ฒด |