JavaScript์์ this
๐ฆ Cukehater
ยท2๋ ์
JavaScript์์ this
๋ ์คํ ์ปจํ
์คํธ๊ฐ ์์ฑ๋ ๋ ๊ฒฐ์ ๋๋๋ฐ, ์คํ ์ปจํ
์คํธ๋ ํจ์๊ฐ ํธ์ถ๋ ๋ ์์ฑ๋๋ฏ๋ก this
๋ ํจ์์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๊ฐ์ด ๋ฌ๋ผ์ง ์ ์๋ค.
์ด๋ฒ ๊ธ์์๋ this
์ ์๋ฏธ์ ๋ค์ํ ์ฌ์ฉ ๋ฐฉ์์ ์์๋ณธ๋ค.
TL;DR
JavaScript์์ this
๋ ํจ์์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ค.
call
, apply
, bind
๋ฉ์๋๋ฅผ ์ฌ์ฉํ์ฌ this
๋ฅผ ๋ช
์์ ์ผ๋ก ๋ฐ์ธ๋ฉํ ์ ์์ผ๋ฉฐ, ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ์ฌ ์ธ๋ถ this
๋ฅผ ๋ด๋ถ ํจ์์์ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๋ค.
ํธ์ถ ํ๊ฒฝ์ ๋ฐ๋ฅธ this
๋ธ๋ผ์ฐ์ ํ๊ฒฝ์์ ์ ์ญ ๊ณต๊ฐ์ this
๋ Window
๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ฉฐ, Node ํ๊ฒฝ์์๋ global
์ ๊ฐ๋ฆฌํจ๋ค.
javascript
console.log(this); // Window {...}
console.log(this === window); // true
ํจ์์์์ this
ํจ์๋ก์ ํธ์ถํ ๊ฒฝ์ฐ, this
๋ Window
๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค์ง๋ง, new
ํค์๋๋ก ์์ฑ์ ํจ์๋ฅผ ํธ์ถํ ๊ฒฝ์ฐ, this
๋ ์์ฑ๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
javascript
const func = function () {
console.log(this);
};
func(); // Window {...}
function PrintThis() {
console.log(this);
}
PrintThis(); // Window {...}
const instance = new PrintThis(); // PrintThis {}
๊ฐ์ฒด์ ๋ฉ์๋์์์ this
๊ฐ์ฒด์ ๋ฉ์๋๋ก ํธ์ถํ ๊ฒฝ์ฐ, this
๋ ํด๋น ๋ฉ์๋๋ฅผ ํธ์ถํ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
javascript
const obj = {
method: function() {
console.log(this);
},
};
obj.method(); // { method: f }
์ด๋ฒคํธ ๋ฆฌ์ค๋์์์ this
์ด๋ฒคํธ ๋ฆฌ์ค๋์์ this
๋ ์ด๋ฒคํธ๊ฐ ๋ฐ์ํ ์์ e.currentTarget
๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
html
<button type="button" id="btn">Button</button>
<script>
document.getElementById('btn').addEventListener('click', function (e) {
console.log(this); // <button type="button" id="btn">Button</button>
console.log(e.currentTarget); // <button type="button" id="btn">Button</button>
});
</script>
this ๋ฐ์ธ๋ฉ
ํจ์์ ํธ์ถ ๋ฐฉ์์ ๋ฐ๋ผ ๋ฌ๋ผ์ง๋ this
๊ฐ์ ๋ช
์์ ์ผ๋ก ๋ฐ์ธ๋ฉํ๊ธฐ ์ํด call
, apply
, bind
๋ฉ์๋๋ฅผ ์ฌ์ฉํ ์ ์๋ค.
1. call
call
๋ฉ์๋๋ ์ธ์๋ก ์ ๋ฌํ ๊ฐ์ฒด๋ฅผ this
๋ก ์ฌ์ฉํ์ฌ ํจ์๋ฅผ ํธ์ถํ๋ค.
javascript
const person = {
name: 'ํ๊ธธ๋',
};
function update(birthYear, job) {
this.birthYear = birthYear;
this.job = job;
}
update.call(person, 1998, '๊ฐ์');
console.log(person); // { name: 'ํ๊ธธ๋', birthYear: 1998, job: '๊ฐ์' }
2. apply
apply
๋ฉ์๋๋ call
๋ฉ์๋์ ๋น์ทํ์ง๋ง, ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ์ ๋ฌํ๋ค.
javascript
const person = {
name: 'ํ๊ธธ๋',
};
function update(birthYear, job) {
this.birthYear = birthYear;
this.job = job;
}
update.apply(person, [1998, '๊ฐ์']);
console.log(person); // { name: 'ํ๊ธธ๋', birthYear: 1998, job: '๊ฐ์' }
3. bind
bind
๋ฉ์๋๋ ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ฉฐ, ๋์ค์ ํธ์ถํ ๋ this
๊ฐ ๋ฐ์ธ๋ฉ๋๋ค.
javascript
const person = {
name: 'ํ๊ธธ๋',
};
function update(birthYear, job) {
this.birthYear = birthYear;
this.job = job;
}
const updatePerson = update.bind(person);
updatePerson(1998, '๊ฐ์');
console.log(person); // { name: 'ํ๊ธธ๋', birthYear: 1998, job: '๊ฐ์' }
ํ์ดํ ํจ์์์์ this
ํ์ดํ ํจ์๋ this
๋ฅผ ์ธ๋ถ ์ปจํ
์คํธ์ this
๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ๊ธฐ ๋๋ฌธ์ ๋ด๋ถ ํจ์์์ ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ๋ฉด ์ธ๋ถ ํจ์์ this
๋ฅผ ๊ทธ๋๋ก ์ฌ์ฉํ ์ ์๋ค.
javascript
const person = {
name: 'ํ๊ธธ๋',
outer: function () {
console.log(this); // { name: 'ํ๊ธธ๋', outer: [Function: outer] }
const inner = () => {
console.log(this); // { name: 'ํ๊ธธ๋', outer: [Function: outer] }
};
inner();
},
};
person.outer();
์ฃผ์ ์ฌํญ
ํ์ดํ ํจ์๋ฅผ ์ฌ์ฉํ ๋ ๋ค์๊ณผ ๊ฐ์ ์ฃผ์ ์ฌํญ์ด ์๋ค.
- ๊ฐ์ฒด์ ๋ฉ์๋๋ก ์ฌ์ฉํ ๊ฒฝ์ฐ,
this
๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
javascript
const person = {
name: 'ํ๊ธธ๋',
printName: () => {
console.log(this); // Window
},
};
person.printName();
- ์ด๋ฒคํธ ๋ฆฌ์ค๋์ ์ฝ๋ฐฑ ํจ์์ ์ฌ์ฉํ ๊ฒฝ์ฐ
this
๋ ์ ์ญ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํจ๋ค.
html
<button type="button" id="btn">Button</button>
<script>
document.getElementById('btn').addEventListener('click', e => {
console.log(this); // Window
});
</script>

๐ฆ Cukehater
๊ฐ๋ฐ ๊ฒฝํ๊ณผ ๊ธฐ์ ์ ์ธ์ฌ์ดํธ๋ฅผ ๊ณต์ ํฉ๋๋ค ๐ปโจ