๐Ÿฆ„ Cukehater

JavaScript์—์„œ this

๐Ÿฆ„ Cukehater

ยท

2๋…„ ์ „

JavaScript์—์„œ this๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ๊ฒฐ์ •๋˜๋Š”๋ฐ, ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ์ƒ์„ฑ๋˜๋ฏ€๋กœ this๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๊ฐ’์ด ๋‹ฌ๋ผ์งˆ ์ˆ˜ ์žˆ๋‹ค.

์ด๋ฒˆ ๊ธ€์—์„œ๋Š” this์˜ ์˜๋ฏธ์™€ ๋‹ค์–‘ํ•œ ์‚ฌ์šฉ ๋ฐฉ์‹์„ ์•Œ์•„๋ณธ๋‹ค.


TL;DR

JavaScript์—์„œ this๋Š” ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง„๋‹ค.

call, apply, bind ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์™ธ๋ถ€ this๋ฅผ ๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.


ํ˜ธ์ถœ ํ™˜๊ฒฝ์— ๋”ฐ๋ฅธ this

๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ ์ „์—ญ ๊ณต๊ฐ„์˜ this๋Š” Window ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ, Node ํ™˜๊ฒฝ์—์„œ๋Š” global์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

javascript Copy
console.log(this); // Window {...}
console.log(this === window); // true

ํ•จ์ˆ˜์—์„œ์˜ this

ํ•จ์ˆ˜๋กœ์„œ ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ, this๋Š” Window ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€๋งŒ, new ํ‚ค์›Œ๋“œ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ, this๋Š” ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

javascript Copy
const func = function () {
  console.log(this);
};
func(); // Window {...}

function PrintThis() {
  console.log(this);
}
PrintThis(); // Window {...}

const instance = new PrintThis(); // PrintThis {}

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์—์„œ์˜ this

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœํ•œ ๊ฒฝ์šฐ, this๋Š” ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

javascript Copy
const obj = {
  method: function() {
    console.log(this);
  },
};
obj.method(); // { method: f }

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์—์„œ์˜ this

์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์—์„œ this๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ e.currentTarget๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

html Copy
<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 Copy
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 Copy
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 Copy
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 Copy
const person = {
  name: 'ํ™๊ธธ๋™',
  outer: function () {
    console.log(this); // { name: 'ํ™๊ธธ๋™', outer: [Function: outer] }
    const inner = () => {
      console.log(this); // { name: 'ํ™๊ธธ๋™', outer: [Function: outer] }
    };
    inner();
  },
};
person.outer();

์ฃผ์˜ ์‚ฌํ•ญ

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ์ฃผ์˜ ์‚ฌํ•ญ์ด ์žˆ๋‹ค.

  1. ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ, this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
javascript Copy
const person = {
  name: 'ํ™๊ธธ๋™',
  printName: () => {
    console.log(this); // Window
  },
};
person.printName();
  1. ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์— ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
html Copy
<button type="button" id="btn">Button</button>
<script>
  document.getElementById('btn').addEventListener('click', e => {
    console.log(this); // Window
  });
</script>
cukehater

๐Ÿฆ„ Cukehater

๊ฐœ๋ฐœ ๊ฒฝํ—˜๊ณผ ๊ธฐ์ˆ ์  ์ธ์‚ฌ์ดํŠธ๋ฅผ ๊ณต์œ ํ•ฉ๋‹ˆ๋‹ค ๐Ÿ’ปโœจ