Zoeeey
Stack flows in you
Zoeeey
  • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (155)
    • ๐Ÿ’ก UXUI (4)
      • UXUI (2)
      • UI (2)
      • UX (0)
    • ๐ŸŸจ JavaScript (66)
      • ๊ฐœ๋… (42)
      • ์ •๋ฆฌ (2)
      • ๋ฌธ์ œํ’€๊ธฐ (ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค, ์ฝ”๋”ฉ์• ํ”Œ) (15)
      • ์‘์šฉํ•˜์—ฌ ์ž๋™ํ™”or๋ฌธ์ œํ•ด๊ฒฐํ•˜๊ธฐ (2)
      • ๊ธฐํƒ€ (5)
    • โš›๏ธ React (49)
      • ๊ฐœ๋… (27)
      • ๊ฐœ๋…(Redux) (3)
      • ์ •๋ฆฌ (4)
      • ๋ฌธ์ œํ’€๊ธฐ (์ฝ”๋”ฉ์• ํ”Œ: React) (15)
    • ๐Ÿ“˜ TypeScript (10)
      • ๊ฐœ๋… (8)
      • ๋ฌธ์ œํ’€๊ธฐ (์ฝ”๋”ฉ์• ํ”Œ) (2)
    • ๐ŸŒ HTML,CSS (18)
      • ์‹ค๋ฌด์—์„œ ์ž์ฃผ ๋ฐœ์ƒํ•œ ์˜ค๋ฅ˜๋“ค (10)
      • ๋’ค๋Šฆ๊ฒŒ ์•Œ์•„์„œ ๋ผˆ์•„ํ”ˆ ์œ ์šฉํ•œ CSS (6)
      • ๊ธฐํƒ€ ์ž์ฃผ ์“ฐ๋Š” CSS (2)
    • ๐Ÿ’ป CS (8)
      • CS (5)
      • ์ •๊ทœํ‘œํ˜„์‹ (2)
      • Git,Github (1)

ํƒœ๊ทธ

  • TypeScript
  • array
  • CSS
  • axios
  • ์ฝ”๋”ฉ์• ํ”Œ
  • object
  • json
  • class
  • Ajax
  • CS
  • JavaScript
  • ์‚ผํ•ญ์—ฐ์‚ฐ์ž
  • redux
  • react
  • Props
  • useEffect
  • UXUI
  • ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค
  • ์˜ค๋ฅ˜
  • state
์ „์ฒด ๋ฐฉ๋ฌธ์ž
์˜ค๋Š˜
์–ด์ œ
hELLO ยท Designed By ์ •์ƒ์šฐ.
Zoeeey

Stack flows in you

๐ŸŸจ JavaScript/์ •๋ฆฌ

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

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

์ €์ž‘์žํ‘œ์‹œ ๋น„์˜๋ฆฌ ๋™์ผ์กฐ๊ฑด (์ƒˆ์ฐฝ์—ด๋ฆผ)

'๐ŸŸจ JavaScript > ์ •๋ฆฌ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

JavaScript ์ •๋ฆฌ : this (2) ํ™”์‚ดํ‘œํ•จ์ˆ˜์—์„œ์˜ this  (0) 2023.08.17
    '๐ŸŸจ JavaScript/์ •๋ฆฌ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
    • JavaScript ์ •๋ฆฌ : this (2) ํ™”์‚ดํ‘œํ•จ์ˆ˜์—์„œ์˜ this
    Zoeeey
    Zoeeey
    ๊ธธ๊ฒŒ ์ƒ๊ฐํ•˜๊ณ  ์งง๊ฒŒ ๊ทธ๋ฆฌ๊ธฐ

    ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”