this
“๊ฐ์ฒด”๋ ์ํ๋ฅผ ๋ํ๋ด๋ ํ๋กํผํฐ์ ๋์์ ๋ํ๋ด๋ ๋ฉ์๋๋ฅผ ํ๋์ ๋ ผ๋ฆฌ์ ์ธ ๋จ์๋ก ๋ฌถ์ ๋ณตํฉ์ ์ธ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
๋์์ ๋ํ๋ด๋ ๋ฉ์๋๋ ์์ ์ด ์ํ ๊ฐ์ฒด์ ์ํ, ์ฆ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐํ๊ณ ๋ณ๊ฒฝํ ์ ์์ด์ผ ํ๋ค. ์ด๋ ๋ฉ์๋๊ฐ ์์ ์ด ์ํ ๊ฐํ์ ํ๋กํผํฐ๋ฅผ ์ฐธ์กฐ ํ๋ ค๋ฉด
๋จผ์ ์์ ์ด ์ํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํค๋ ์๋ณ์๋ฅผ ์ฐธ์กฐํ ์ ์์ด์ผ ํ๋ค.
const circle = {
radius: 5,
getDiameter() {
return 2 * Circle.radius
}
};
console.log(circle.getDiameter()) // 10
์์ ์ค๋ช ์ ๋ฃ๊ณ ์ฝ๋๋ฅผ ๋ณด๋ ๋ฌด์จ๋ง์ธ์ง ์ดํด ๋์๋์ฌ?
- ๋ฉ์๋๋ ๊ธฐ๋ณธ์ ์ผ๋ก ๊ฐ์ฒด์ ํ๋กํผํฐ๋ฅผ ๋ณ๊ฒฝํฉ๋๋ค. ์ฆ, ๋ณ์(์ํ)๋ฅผ ๋ณ๊ฒฝ์ํค๋ ์ผ์ ํฉ๋๋ค.
- ๊ทธ๋ฐ๋ฐ ๊ฐ์ฒด์์ ๋ฉ์๋๊ฐ ๋ณ์์ ์ ๊ทผ์ ํ๊ธฐ์ํด์๋ ์์ ์ ์ฌ๊ท์ ์ผ๋ก ์ฐธ์กฐํด์ผํ๋ ์ผ์ด ๋ฐ์ํฉ๋๋ค.
์ด๋ด ๋ ์ฌ์ฉํ๋ ๊ฒ์ด this ํค์๋ ์ ๋๋ค!.
์ด๋ฌํ this๊ฐ ๊ฐ๋ฅดํค๋ ๊ฐ์ ํจ์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋์ ์ผ๋ก ๊ฒฐ์ ๋ฉ๋๋ค.
ํธ์ถ ๋ฐฉ์์ ํฌ๊ฒ 3๊ฐ์ง ๊ฒฝ์ฐ๋ก ๋๋ฉ๋๋ค.
- ์ผ๋ฐ ํจ์ ํธ์ถ
- ๋ฉ์๋ ํธ์ถ
- ์์ฑ์ ํจ์ ํธ์ถ
์ผ๋ฐ ํจ์ํธ์ถ
๊ธฐ๋ณธ์ ์ผ๋ก ์ผ๋ฐ ํจ์ ํธ์ถ์ this๋ window ์ฆ, ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋ฉ๋๋ค.
function foo() {
console.log(this) //window
}
๋ฉ์๋์์ ์ค์ฒฉํจ์๋ก ์ ์ธ๋ ์ผ๋ฐํจ์ ์ญ์ this๋ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋ฉ๋๋ค.
const obj = {
value: 100,
foo() {
console.log('foo`s this: ', this); //window
console.log('foo`s this.value: ', this.value); // 100
function bar() {
console.log('callback`s this', this) //window
console.log('callback`s this.value', this.value) //1
};
}
}
์ฝ๋ฐฑ ํจ์๊ฐ ์ผ๋ฐํจ์๋ก ํธ์ถ ๋๋ค๋ฉด ์ฝ๋ฐฑ ํจ์ ๋ด๋ถ์ this ์ญ์ ์ ์ญ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋ฉ๋๋ค.
const obj = {
value: 100,
foo() {
console.log('foo`s this: ', this) //window
setTimeout(() => {
console.log('callback`s this', this) //window
console.log('callback`s this.value', this.value) //1
}, 100);
์ด์ฒ๋ผ ์ผ๋ฐ ํจ์๋ก ํธ์ถ๋ ๋ชจ๋ ํจ์ ๋ด๋ถ์ this์๋ ์ ์ญ ๊ฐ์ฒด๊ฐ ๋ฐ์ธ๋ฉ ๋ฉ๋๋ค.
๊ทธ๋ฐ๋ฐ ์ซ ์ด์ํ์ง ์๋์???
์ค์ฒฉํจ์๋ ์ฝ๋ฐฑํจ์๋ ์ธ๋ถ ํจ์๋ฅผ ๋๋ ํฌํผํจ์์ธ๋ฐ, this๊ฐ ์ธ๋ถํจ์๋ฅผ ๊ฐ๋ฅดํค๊ณ ์๋ ๊ฒ์ด ํฉ๋นํ์ง ์์๊น์?
๊ทธ๋ฐ ์๊ฐ์ผ๋ก ๋ง๋ค์ด์ง ๋ฉ์๋๊ฐ ์์ต๋๋ค. ๋ค์ ์ค๋ช ํ apply, call, bind์ ๋๋ค.
const obj = {
value: 100,
foo() {
const that = this;
setTimeout(() => {
console.log('callback`s this.value', that.value) //100
}, 100);
}
}
์ด๋ฐ์์ ํ ํฌ๋๋ ์์ต๋๋ค.
๋ฉ์๋์ ํธ์ถ
๋ฉ์๋์ this๋ ์ผ๋ฐ์ ์ผ๋ก ์์ ์ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํต๋๋ค.
const person = {
name: 'lee',
getName() {
return this.name;
}
};
console.log(person.getName()); //lee
์์ฑ์ ํจ์ ํธ์ถ
์์ฑ์ ํจ์ ๋ด๋ถ์ this์๋ ์์ฑ์ ํจ์๊ฐ ์์ฑํ ์ธ์คํด์ค๊ฐ ๋ฐ์ธ๋ฉ ๋ฉ๋๋ค.
์ฆ, ์์ฑ์ ํจ์๊ฐ ๋ง๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํค๊ฒ ๋ฉ๋๋ค..
function Circle(radius) {
this.radius = radius;
this.getDiameter = function () {
return 2 * this.radius;
};
}
const circle1 = new Circle(5);
console.log(circle1.getDiameter()); //10ใ
์์ฑ์ ํจ์๋ฅผ newํค์๋ ์์ด ์์ฑํ ํจ์๋ ์ผ๋ฐํจ์๊ฐ ๋๋๋ฐ, ๊ทธ๋ด๊ฒฝ์ฐ ์ ์ญ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฅดํต๋๋ค.
Function.prototype.apply/call/bind ๋ฉ์๋์ ์ํ ๊ฐ์ ํธ์ถ
์ค์ฒฉ ํจ์๋ ์ฝ๋ฐฑ ํจ์๊ฐ ๋ฉ์๋ ๋ด์ ์์ผ๋ฉด ์ธ๋ถํจ์๊ฐ ์๋๋ผ ์ ์ญ์ ๊ฐ๋ฅดํจ๋ค๊ณ ์ค๋ช ๋๋ ธ์ฃ ?.
๋ ๊ทธ๊ฒ ๊ต์ฅํ ์ด์ํ ๊ฒ์ด๋ผ๊ณ ๋ ์ค๋ช ๋๋ ธ์ต๋๋ค.
์ด์ ๊ทธ ์ด์ํจ์ ํ์ด ๋ณด๊ฒ ์ต๋๋ค.
appply, call
๋์ ์ฌ์ฉ๋ฒ์ ์ฌ์ค์ ๊ฐ๋ค๊ณ ์๊ฐํ์ ๋ ๋ฉ๋๋ค.
๋ ๋ฉ์๋๋ this๋ก ์ฌ์ฉํ ๊ฐ์ฒด์ ์ธ์ ๋ฆฌ์คํธ(arguments)๋ฅผ ์ธ์๋ก ์ ๋ฌ ๋ฐ์ ํจ์๋ฅผ ํธ์ถํฉ๋๋ค.
์ฌ์ฉ๋ฒ:
function getThisBinding() {
return this;
}
const thisArg = {a: 1};
console.log(getThisBinding()) //window
console.log(getThisBinding.apply(thisArg)) // {a: 1}
console.log(getThisBinding.call(thisArg)) // {a: 1}
์ฌ์ฉ๋ฒ์ด ๊ฐ๋จํ์ฃ ?
์ธ์๋ฅผ ๋ฃ์ด ์ฃผ๊ณ ์ถ์ ๋. ์๋์ ๋ฐฉ๋ฒ์ผ๋ก ์ฌ์ฉํฉ๋๋ค.
function getThisBinding() {
return this;
}
const thisArg = {a: 1};
console.log(getThisBinding()) //window
console.log(getThisBinding.apply(thisArg, [1, 2, 3]))
console.log(getThisBinding.call(thisArg, 1, 2, 3))๋ฐ
as
bind
bind ๋ฉ์๋๋ ๋ฉ์๋์ this์ ๋ฉ์๋ ๋ด๋ถ์ ์ค์ฒฉํจ์ ๋๋ ์ฝ๋ฐฑ ํจ์์ this๊ฐ ๋ถ์ผ์น ํ๋ ๋ฌธ์ ๋ฅผ ํด๊ฒฐ ํ๊ธฐ ์ํด ์ ์ฉํ๊ฒ ์ฌ์ฉ๋ฉ๋๋ค.
const person = {
name: 'lee',
foo(callback) {
setTimeout(callback, 100);
}
}
person.foo(function () {
console.log(`name: ${this.name}.`); // name: .
// window.name์ ๋ธ๋ผ์ฐ์ ์ฐฝ์ ๋ํ๋ด๋ ๋นํธ์ธ ํ๋กํผํฐ์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ์ ''์
๋๋ค.
});
const person = {
name: 'lee',
foo(callback) {
setTimeout(callback.bind(this), 100);
}
}
person.foo(function () {
console.log(`name: ${this.name}.`); // name: lee.
// window.name์ ๋ธ๋ผ์ฐ์ ์ฐฝ์ ๋ํ๋ด๋ ๋นํธ์ธ ํ๋กํผํฐ์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ์ ''์
๋๋ค.
};
์ด๋ฐ ์์ผ๋ก ๋ง์ด์ฃ !.
๋ค๋ฅธ ๊ฐ์ฒด์งํฅ ์ธ์ด์ ๋ฌ๋ฆฌ js์ this๋ ์ํฉ์ ๋ฐ๋ผ ์ฐธ ๋์ ์ผ๋ก ๋ณํด ์ด๋ ค์์ ์ค๋๋ค.
์ด๊ฒ js์ ๋งค๋ ฅ์ธ๊ฐ ์ถ๊ธฐ๋ ํฉ๋๋ค.
์ด ์ ๋ฆฌ๋ ์๋ฐ์คํฌ๋ฆฝํธ ๋ฅ๋ค์ด๋ธ๋ฅผ ๋ฐฐ๊ฒฝ์ผ๋ก ํ๊ณ ์์ต๋๋ค. ๋์์ด ๋ง์ด ๋๋ ์ฑ ์ด๊ธฐ์ js๋ฅผ ๊ณต๋ถํ์๋ ๋ถ๋ค์ด๋ผ๋ฉด ๊ผญ ๋ณด์๊ธธ~~.
๋ this์ ๋ํ ์ฌ๋ฐ๋ ์ ๋ฆฌ๊ฐ ์๋๋ฐ ์๊ฐ์ด ์์ผ์๋ฉด ๊ผฎใฒใฒ ๋ณด์ธ์ฌ
์๋ฐ์คํฌ๋ฆฝํธ๋ ์ ํ๋กํ ํ์ ์ ์ ํํ์๊น
ํ๋กํ ํ์ ์ผ๋ก ๊ฒ์ํ๋ฉด ์ผ๋ ๋์ค๋ ์๋์ฒ๋ผ ์ ๋ํ ์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฒ์ ์ ํ์ ๋ ๊ฐ์ฅ ๋นํฉ์ค๋ฌ์ ๋ ๊ฒ ํ๋กํ ํ์ ์ด์์ต๋๋ค.
medium.com