๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Language/Javascript

[Javascript]์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this ์™„๋ฒฝ ๊ฐ€์ด๋“œ: ๊ฐœ๋…๋ถ€ํ„ฐ ํ™œ์šฉ๊นŒ์ง€!

by YJ Dev 2024. 7. 5.
728x90
๋ฐ˜์‘ํ˜•
SMALL

JavaScript๋ฅผ ๋ฐฐ์šฐ๋‹ค ๋ณด๋ฉด this๋ผ๋Š” ํ‚ค์›Œ๋“œ๋ฅผ ์ž์ฃผ ์ ‘ํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. this๋Š” ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋˜๋Š” ํŠน๋ณ„ํ•œ ํ‚ค์›Œ๋“œ๋กœ, ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ๊ฒฐ์ •๋˜๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ์ œ๋Œ€๋กœ ์ดํ•ดํ•˜์ง€ ๋ชปํ•˜๋ฉด ์ฝ”๋“œ๊ฐ€ ์˜๋„์น˜ ์•Š๊ฒŒ ๋™์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this๊ฐ€ ์–ด๋–ป๊ฒŒ ๋™์ž‘ํ•˜๋Š”์ง€, ๋‹ค์–‘ํ•œ ์ƒํ™ฉ์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉ๋˜๋Š”์ง€ ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this ํ‚ค์›Œ๋“œ


์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ this๋Š” ํ•จ์ˆ˜ ๋‚ด์—์„œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค. ํ•จ์ˆ˜์™€ ๊ด€๋ จ๋œ ์ž์„ธํ•œ ๋‚ด์šฉ์€ ์•„๋ž˜ ํฌ์ŠคํŒ…์„ ์ฐธ๊ณ ํ•ด ์ฃผ์„ธ์š”๐Ÿ˜

""

[Javascript]์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜ ์™„๋ฒฝ ๊ฐ€์ด๋“œ: ์ดˆ๋ณด์ž๋ฅผ ์œ„ํ•œ ํ•„์ˆ˜ ํŒ๊ณผ ์˜ˆ์ œ

์ด๋ฒˆ ํฌ์ŠคํŒ…์—์„œ๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ•ต์‹ฌ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜์ธ ํ•จ์ˆ˜์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜๋Š” ์›น ๊ฐœ๋ฐœ์—์„œ ๋งค์šฐ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธ€์„ ํ†ตํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ํ•จ์ˆ˜์˜ ๊ธฐ๋ณธ

creativevista.tistory.com


01. this์˜ ๋™์ž‘ ์›๋ฆฌ์™€ ๊ธฐ๋ณธ ๊ฐœ๋…๐Ÿง 

๊ธฐ๋ณธ์ ์ธ this์˜ ๋™์ž‘ ๋ฐฉ์‹

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ this๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ ์ฐธ์กฐํ•˜๋Š” ๋Œ€์ƒ์ด ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ „์—ญ ์ปจํ…์ŠคํŠธ์™€ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ ๊ฐ๊ฐ ๋‹ค๋ฅด๊ฒŒ ๋™์ž‘ํ•˜์ฃ .

console.log(this); // ์ „์—ญ ๊ฐ์ฒด (๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” window ๊ฐ์ฒด)

์ „์—ญ ์ปจํ…์ŠคํŠธ์—์„œ์˜ this

์ „์—ญ ์ปจํ…์ŠคํŠธ์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค. ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ๋Š” window ๊ฐ์ฒด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

function globalThis() {
    console.log(this);
}
globalThis(); // window ๊ฐ์ฒด

ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

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

function regularFunction() {
    console.log(this);
}
regularFunction(); // window ๊ฐ์ฒด (์—„๊ฒฉ ๋ชจ๋“œ์—์„œ๋Š” undefined)

02. ๋ฉ”์„œ๋“œ์—์„œ์˜ this ์‚ฌ์šฉ๋ฒ•๐Ÿ’ผ

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

๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœ๋œ ํ•จ์ˆ˜์—์„œ๋Š” ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

const obj = {
    name: 'JavaScript',
    getName: function() {
        console.log(this.name);
    }
};
obj.getName(); // 'JavaScript'

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

์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ƒˆ๋กœ์šด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ์‚ฌ์šฉ๋˜๋ฉฐ, this๋Š” ์ƒ์„ฑ๋˜๋Š” ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

function Person(name) {
    this.name = name;
}
const person = new Person('John');
console.log(person.name); // 'John'

03. ์ฝœ๋ฐฑ ํ•จ์ˆ˜์™€ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ์˜ this๐ŸŽ›๏ธ

์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ์˜ this

์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this๋Š” ํ˜ธ์ถœ๋˜๋Š” ์ปจํ…์ŠคํŠธ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง‘๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ช…ํ™•ํžˆ ํ•˜๊ธฐ ์œ„ํ•ด bind, apply, call ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๋ฉ”์„œ๋“œ์˜ ์‚ฌ์šฉ๋ฒ•๊ณผ ํŠน์ง•์„ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

  • bind: this ๊ฐ’์„ ์„ค์ •ํ•œ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  • call: ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ this ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ , ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.
  • apply: ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ this ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ , ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

bind ๋ฉ”์„œ๋“œ

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

const obj = {
  name: 'Alice',
  getName: function() {
    console.log(this.name);
  }
};

const callback = obj.getName.bind(obj);
callback(); // 'Alice' ์ถœ๋ ฅ

const anotherObj = {
  name: 'Bob',
  getName: callback
};
anotherObj.getName(); // 'Alice' ์ถœ๋ ฅ (bind๋กœ ์„ค์ •๋œ this๊ฐ€ ์œ ์ง€๋จ)

์˜ˆ์ œ: bind๋ฅผ ์‚ฌ์šฉํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ์˜ this ์ œ์–ด

const obj = {
  name: 'Alice',
  getName: function() {
    console.log(this.name);
  }
};

function executeCallback(cb) {
  cb();
}

const boundCallback = obj.getName.bind(obj);
executeCallback(boundCallback); // 'Alice' ์ถœ๋ ฅ

call ๋ฉ”์„œ๋“œ

call ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ this ๊ฐ’์„ ๋ช…์‹œ์ ์œผ๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ this ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ , ๊ทธ ๋’ค์— ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•˜์—ฌ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

function greet(greeting) {
  console.log(greeting + ', ' + this.name);
}

const person = { name: 'Alice' };
greet.call(person, 'Hello'); // 'Hello, Alice' ์ถœ๋ ฅ

์˜ˆ์ œ: call์„ ์‚ฌ์šฉํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ์˜ this ์ œ์–ด

const obj = {
  name: 'Alice',
  getName: function() {
    console.log(this.name);
  }
};

function executeCallback(cb) {
  cb.call(obj); // ๋ช…์‹œ์ ์œผ๋กœ this ์„ค์ •
}

executeCallback(obj.getName); // 'Alice' ์ถœ๋ ฅ

apply ๋ฉ”์„œ๋“œ

apply ๋ฉ”์„œ๋“œ๋Š” call๊ณผ ์œ ์‚ฌํ•˜์ง€๋งŒ, ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค. ์ฒซ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ this ๊ฐ’์„ ์„ค์ •ํ•˜๊ณ , ๋‘ ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ธ์ˆ˜ ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•ฉ๋‹ˆ๋‹ค.

function greet(greeting, punctuation) {
  console.log(greeting + ', ' + this.name + punctuation);
}

const person = { name: 'Alice' };
greet.apply(person, ['Hello', '!']); // 'Hello, Alice!' ์ถœ๋ ฅ

์˜ˆ์ œ: apply๋ฅผ ์‚ฌ์šฉํ•œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์—์„œ์˜ this ์ œ์–ด

const obj = {
  name: 'Alice',
  getName: function() {
    console.log(this.name);
  }
};

function executeCallback(cb) {
  cb.apply(obj); // ๋ช…์‹œ์ ์œผ๋กœ this ์„ค์ •
}

executeCallback(obj.getName); // 'Alice' ์ถœ๋ ฅ

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ์˜ this

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ this๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ DOM ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. ์ด ๊ธฐ๋ณธ ๋™์ž‘์€ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋ธŒ๋ผ์šฐ์ €๊ฐ€ this๋ฅผ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ๋กœ ์„ค์ •ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‹ค์–‘ํ•œ ๋ฐฉ๋ฒ•์„ ํ†ตํ•ด this๋ฅผ ๋‹ค๋ฅธ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•˜๊ฑฐ๋‚˜ ์กฐ์ž‘ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ ๊ฒฝ์šฐ์™€ ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ธฐ๋ณธ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ํ•จ์ˆ˜์—์„œ this๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ DOM ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

const button = document.getElementById('myButton');
button.addEventListener('click', function() {
  console.log(this); // ํด๋ฆญ๋œ ๋ฒ„ํŠผ ์š”์†Œ๋ฅผ ์ถœ๋ ฅ
});

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ์„œ์˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ž์‹ ๋งŒ์˜ this๋ฅผ ๊ฐ€์ง€์ง€ ์•Š๊ณ , ์ •์˜๋œ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ์‚ฌ์šฉํ•˜๋ฉด ์˜๋„ํ•˜์ง€ ์•Š์€ this๊ฐ€ ์ฐธ์กฐ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const button = document.getElementById('myButton');
button.addEventListener('click', (event) => {
  console.log(this); // ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ถœ๋ ฅ (์ „์—ญ ๊ฐ์ฒด ๋˜๋Š” undefined)
});

bind๋ฅผ ์ด์šฉํ•œ this ์„ค์ •

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ this๋ฅผ ํŠน์ • ๊ฐ์ฒด๋กœ ์„ค์ •ํ•˜๊ณ  ์‹ถ๋‹ค๋ฉด bind ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const obj = {
  name: 'Alice',
  handleClick: function() {
    console.log(this.name); // 'Alice'๋ฅผ ์ถœ๋ ฅ
  }
};

const button = document.getElementById('myButton');
button.addEventListener('click', obj.handleClick.bind(obj));

call๊ณผ apply๋ฅผ ์ด์šฉํ•œ this ์„ค์ •

call๊ณผ apply๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์ง€๋งŒ, ์ด๋Ÿฌํ•œ ๋ฐฉ๋ฒ•์€ ์ง์ ‘ ์ด๋ฒคํŠธ ๋ฆฌ์Šค๋„ˆ์— ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ๋“œ๋ญ…๋‹ˆ๋‹ค. ๋Œ€์‹  ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด์—์„œ ๋‹ค๋ฅธ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

const obj = {
  name: 'Alice',
  handleClick: function() {
    console.log(this.name); // 'Alice'๋ฅผ ์ถœ๋ ฅ
  }
};

const button = document.getElementById('myButton');
button.addEventListener('click', function(event) {
  obj.handleClick.call(obj);
});

์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ์˜ this๋ฅผ ์ œ์–ดํ•˜๋Š” ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•

1. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์—์„œ this ์ €์žฅํ•˜๊ธฐ

const obj = {
  name: 'Alice',
  handleClick: function() {
    console.log(this.name);
  }
};

const button = document.getElementById('myButton');
button.addEventListener('click', function(event) {
  const self = this; // ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ ์š”์†Œ๋ฅผ ์ฐธ์กฐ
  obj.handleClick.call(obj); // 'Alice' ์ถœ๋ ฅ
  console.log(self); // ํด๋ฆญ๋œ ๋ฒ„ํŠผ ์š”์†Œ๋ฅผ ์ถœ๋ ฅ
});

2. ๋ฐ์ดํ„ฐ ์†์„ฑ์„ ์‚ฌ์šฉํ•˜์—ฌ this ์ „๋‹ฌ

HTML:

<button id="myButton" data-name="Alice">Click me</button>


JavaScript:

const button = document.getElementById('myButton');
button.addEventListener('click', function(event) {
  const name = this.dataset.name; // data-name ์†์„ฑ ๊ฐ’ ๊ฐ€์ ธ์˜ค๊ธฐ
  console.log(name); // 'Alice' ์ถœ๋ ฅ
});

์š”์•ฝ

  • ๊ธฐ๋ณธ์ ์œผ๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ this๋Š” ์ด๋ฒคํŠธ๊ฐ€ ๋ฐœ์ƒํ•œ DOM ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋ฅผ ์ž‘์„ฑํ•˜๋ฉด this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.
  • bind ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ this๋ฅผ ํŠน์ • ๊ฐ์ฒด๋กœ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • call๊ณผ apply๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ this๋ฅผ ์„ค์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์ œ์–ดํ•˜๊ธฐ ์œ„ํ•ด self๋ฅผ ์‚ฌ์šฉํ•˜๊ฑฐ๋‚˜, HTML ๋ฐ์ดํ„ฐ ์†์„ฑ์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

04. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ์˜ this๐Ÿ“

JavaScript์—์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ this์˜ ๋™์ž‘ ๋ฐฉ์‹์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ž์ฒด์ ์ธ this ๋ฐ”์ธ๋”ฉ์„ ๊ฐ–์ง€ ์•Š๊ณ , ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋œ ๋ฌธ๋งฅ(context)์„ ์œ ์ง€ํ•˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this๊ฐ€ ํ˜ผ๋™๋  ์ˆ˜ ์žˆ๋Š” ์ƒํ™ฉ์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” this๊ฐ€ ์ƒ์œ„ ์Šค์ฝ”ํ”„๋ฅผ ์ฐธ์กฐํ•˜๋ฏ€๋กœ, ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋‚˜ ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ this ๋ฐ”์ธ๋”ฉ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋Š” ์žฅ์ ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

์ฃผ์š” ํŠน์ง•

1. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ž์‹ ์˜ this๋ฅผ ๊ฐ€์ง€์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋Š” ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ this๊ฐ€ ๋‹ฌ๋ผ์ง€์ง€๋งŒ, ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ •์˜๋  ๋•Œ์˜ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

2. ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์ •์˜๋œ ์œ„์น˜์˜ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this์™€ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ์™€ ์„ค๋ช…

1. ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ this

const obj = {
  name: 'Alice',
  regularFunction: function() {
    console.log(this.name); // 'Alice'
  }
};

obj.regularFunction(); // 'Alice' ์ถœ๋ ฅ

2. ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์™€ this

const obj = {
  name: 'Alice',
  arrowFunction: () => {
    console.log(this.name); // ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ `this`๋ฅผ ์ฐธ์กฐ
  }
};

obj.arrowFunction(); // ์ƒ์œ„ ์Šค์ฝ”ํ”„๊ฐ€ ์ „์—ญ ๊ฐ์ฒด์ด๋ฉด `undefined` ์ถœ๋ ฅ

3. ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ƒ์†๋ฐ›๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค. ์ด ์˜ˆ์ œ์—์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ๊ฐ์ฒด ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์ •์˜๋˜์—ˆ์œผ๋ฏ€๋กœ, ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋Š” ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

const obj = {
  name: 'Alice',
  regularFunction: function() {
    const arrowFunction = () => {
      console.log(this.name); // 'Alice'
    };
    arrowFunction();
  }
};

obj.regularFunction(); // 'Alice' ์ถœ๋ ฅ

4. ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ์„œ์˜ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜

์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋„ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์œ ์ง€ํ•ฉ๋‹ˆ๋‹ค.

const obj = {
  name: 'Alice',
  init: function() {
    document.getElementById('myButton').addEventListener('click', () => {
      console.log(this.name); // 'Alice'
    });
  }
};

obj.init();

์ด ์˜ˆ์ œ์—์„œ click ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ๋กœ ์‚ฌ์šฉ๋œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” init ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ์˜ this๋ฅผ ์œ ์ง€ํ•˜๋ฏ€๋กœ, this.name์€ 'Alice'๋ฅผ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.


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

1. ๊ธ€๋กœ๋ฒŒ ์ปจํ…์ŠคํŠธ์—์„œ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๊ฐ€ ์ „์—ญ ์ปจํ…์ŠคํŠธ์—์„œ ์ •์˜๋œ ๊ฒฝ์šฐ, this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

const arrowFunction = () => {
  console.log(this); // ์ „์—ญ ๊ฐ์ฒด (๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” `window`, Node.js์—์„œ๋Š” `global`)
};

arrowFunction();

2. ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด ๋ฉ”์„œ๋“œ๋กœ์„œ์˜ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ํ”ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ์ด ๊ฒฝ์šฐ, this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ฐธ์กฐํ•˜๊ฒŒ ๋˜์–ด ์˜ˆ์ƒ์น˜ ๋ชปํ•œ ๊ฒฐ๊ณผ๋ฅผ ์ดˆ๋ž˜ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const obj = {
  name: 'Alice',
  arrowFunction: () => {
    console.log(this.name); // ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ `this`๋ฅผ ์ฐธ์กฐ (์ „์—ญ ๊ฐ์ฒด)
  }
};

obj.arrowFunction(); // `undefined` ์ถœ๋ ฅ

์š”์•ฝ

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ž์‹ ์˜ this ๋ฐ”์ธ๋”ฉ์„ ๊ฐ€์ง€์ง€ ์•Š๊ณ , ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์ƒ์†๋ฐ›์Šต๋‹ˆ๋‹ค.
  • ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ์œ ์ง€ํ•˜์—ฌ ํ˜ผ๋™์„ ํ”ผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  • ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์€ ๋ฐ”๋žŒ์งํ•˜์ง€ ์•Š์œผ๋ฉฐ, ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ‘œํ˜„์‹์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค.

05. this๋ฅผ ์‚ฌ์šฉํ•œ ์˜ˆ์ œ๐Ÿ”ง

์˜ˆ์ œ 1: ๊ฐ์ฒด ๋‚ด๋ถ€์˜ ๋ฉ”์„œ๋“œ์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

// ๊ฐ์ฒด ์ƒ์„ฑ
const person = {
  firstName: 'John',
  lastName: 'Doe',
  fullName: function() {
    // ์—ฌ๊ธฐ์„œ this๋Š” person ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    return this.firstName + ' ' + this.lastName;
  }
};

// fullName ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
console.log(person.fullName()); // ์ถœ๋ ฅ: John Doe

์œ„ ์˜ˆ์ œ์—์„œ this๋Š” person ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค. person.fullName()์ด ํ˜ธ์ถœ๋  ๋•Œ, this๋Š” person ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ this.firstName๊ณผ this.lastName๋Š” John๊ณผ Doe๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ 2: ์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ •์˜
function Car(make, model) {
  this.make = make;
  this.model = model;
  this.getCarInfo = function() {
    // ์—ฌ๊ธฐ์„œ this๋Š” ์ƒ์„ฑ๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    return this.make + ' ' + this.model;
  };
}

// Car ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๊ฐ์ฒด ์ƒ์„ฑ
const myCar = new Car('Toyota', 'Corolla');

// getCarInfo ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
console.log(myCar.getCarInfo()); // ์ถœ๋ ฅ: Toyota Corolla

์œ„ ์˜ˆ์ œ์—์„œ this๋Š” Car ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•œ ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค. myCar.getCarInfo()๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ this๋Š” myCar ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฏ€๋กœ this.make๊ณผ this.model์€ Toyota์™€ Corolla๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ 3: ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ this์˜ ๋™์  ๋ฐ”์ธ๋”ฉ

// ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์ •์˜
function greet() {
  console.log('Hello, ' + this.name);
}

// ๋‹ค์–‘ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ
const obj1 = { name: 'Alice' };
const obj2 = { name: 'Bob' };

// ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ this์— ๋ฐ”์ธ๋”ฉ
greet.call(obj1); // ์ถœ๋ ฅ: Hello, Alice
greet.call(obj2); // ์ถœ๋ ฅ: Hello, Bob

์œ„ ์˜ˆ์ œ์—์„œ call() ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ greet() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ this๋ฅผ obj1๊ณผ obj2๋กœ ๊ฐ๊ฐ ๋ฐ”์ธ๋”ฉํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ํ†ตํ•ด greet() ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this.name์€ ๊ฐ ๊ฐ์ฒด์˜ name ์†์„ฑ ๊ฐ’์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

728x90
๋ฐ˜์‘ํ˜•