๐ŸŸจ JavaScript/์ •๋ฆฌ

JavaScript ์ •๋ฆฌ : this (1) ์ƒํ™ฉ๋ณ„ this

Zoeeey 2023. 8. 16. 21:50

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


   ๋ชฉ์ฐจ

  1. ์ „์—ญ ์ปจํ…์ŠคํŠธ (Global scope) & ํ•จ์ˆ˜ ํ˜ธ์ถœ
  2. ๊ฐ์ฒด์— ์†ํ•œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ
  3. ๊ฐ์ฒด์— ์†ํ•œ ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ ํ•จ์ˆ˜ ํ˜ธ์ถœ
  4. ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœ (call, apply, bind)
  5. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ
  6. ํด๋ž˜์Šค
  7. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ
  8. "use strict" ๋ชจ๋“œ

1. ์ „์—ญ ์ปจํ…์ŠคํŠธ (Global scope) & ํ•จ์ˆ˜ ํ˜ธ์ถœ

this = ์ „์—ญ ๊ฐ์ฒด

(๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” window)

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

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

2. ๊ฐ์ฒด์— ์†ํ•œ ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

this = ๋ฉ”์„œ๋“œ๊ฐ€ ์†ํ•œ ๊ฐ์ฒด

const obj = {
  method: function() {
    console.log(this);
  }
};

obj.method(); // obj ๊ฐ์ฒด

3. ๊ฐ์ฒด์— ์†ํ•œ ๋ฉ”์„œ๋“œ์˜ ๋‚ด๋ถ€ํ•จ์ˆ˜ ํ˜ธ์ถœ

this = ์ „์—ญ ๊ฐ์ฒด

(๋‚ด๋ถ€ ํ•จ์ˆ˜์—์„œ๋Š” ๋ฉ”์„œ๋“œ์˜ this์™€ ๋‹ค๋ฅธ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ด, ์ฃผ์˜ ํ•„์š”)
์ „์—ญ ์Šค์ฝ”ํ”„์—์„œ ์‹คํ–‰๋˜๋ฉฐ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— this๋Š” ์ „์—ญ๊ฐ์ฒด๊ฐ€ ๋œ๋‹ค.

const obj = {
  outerMethod: function() {
    const innerMethod = function() {
      console.log(this); // ์ „์—ญ ๊ฐ์ฒด (๋ธŒ๋ผ์šฐ์ €์—์„œ๋Š” window)
    };
    innerMethod();
  }
};

obj.outerMethod();

4. ํ•จ์ˆ˜๋ฅผ ๋ฉ”์„œ๋“œ๋กœ ํ˜ธ์ถœ (call, apply, bind)

this = ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๋„˜๊ธด ๊ฐ์ฒด

function greet() {
  console.log(`Hello, ${this.name}`);
}

const person = { name: 'Alice' };

greet.call(person); // Hello, Alice
greet.apply(person); // Hello, Alice

const greetPerson = greet.bind(person);
greetPerson(); // Hello, Alice

5. ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ

this = ์ƒ์„ฑ์ž๋กœ ์ƒ์„ฑ๋œ ์ƒˆ๋กœ์šด ๊ฐ์ฒด

function MyClass() {
  console.log(this);
}

const instance = new MyClass(); // MyClass ์ธ์Šคํ„ด์Šค

6. ํด๋ž˜์Šค

this = ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค

(ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด)

class Dog {
  constructor(name) {
    this.name = name;
  }

  bark() {
    console.log(`${this.name} is barking`);
  }
}

const dog = new Dog('Buddy');
dog.bark(); // Buddy is barking

7. ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

this = ์ด๋ฒคํŠธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚จ DOM ์š”์†Œ

(currentTarget๊ณผ ๋™์ผ)

const button = document.querySelector('button');

button.addEventListener('click', function() {
  console.log(this); // <button id="button">๋ฒ„ํŠผ</button>
  console.log(e.currentTarget); // ์œ„์™€ ๋™์ผ
});

8. use strict ๋ชจ๋“œ

this = ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ "use strict" ๋ชจ๋“œ ์‚ฌ์šฉ ์‹œ, this๊ฐ€ undefined๋กœ ์„ค์ •๋จ

(์›๋ž˜ ์ „์—ญ ๊ฐ์ฒด์ธ ๊ฒฝ์šฐ)

"use strict";

function strictModeFunction() {
  console.log(this); // undefined
}

strictModeFunction();

์ถœ์ฒ˜ : MDN, https://sangjuntech.tistory.com/24, ZeroCho, chatGPT