๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Web/JavaScript

[JavaScript] this ํ‚ค์›Œ๋“œ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“’ ์™œ? 

โ—ผ JavaScript๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ this๋ฅผ ์‚ฌ์šฉํ–ˆ์„ ๋•Œ, ์–ด๋–ค ๊ฒฝ์šฐ์—๋Š” {window}๊ฐ€, ๋˜ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” Object๊ฐ€ ์ถœ๋ ฅ๋˜์—ˆ๋‹ค.

โ—ผ ์ด์— this์— ๋Œ€ํ•ด ์ •๋ฆฌํ•˜๊ณ ์ž ๊ธ€์„ ์ž‘์„ฑํ•œ๋‹ค.


๐Ÿ“’ this ํ‚ค์›Œ๋“œ

โ—ผ this๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ์„œ ๋œป์ด 3~4 ๊ฐ€์ง€๋กœ ๋ณ€๊ฒฝ๋œ๋‹ค.

 

โœ” ์ตœ์ƒ์œ„ this & function์—์„œ this ์‚ฌ์šฉ

<script>
  console.log(this);
</script>
<script>
  function test() {
    console.log(this);
  }
  test();
</script>

โ—ผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” window์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค.

โ—ผ window๋Š” ์ „์—ญ ๋ณ€์ˆ˜ ๋ณด๊ด€์†Œ์ด๋‹ค.

 

โœ” strict mode์—์„œ function์—์„œ this ์‚ฌ์šฉ

<script>
  "use strict";
  function test() {
    console.log(this);
  }
  test();
</script>

โ—ผ strict mode (์—„๊ฒฉํ•œ ์‹คํ–‰)์—์„œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ์•ˆ์— this๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด undefined๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

โœ” Object ์•ˆ์˜ function์—์„œ this ์‹คํ–‰

<script>
  var obj = {
    name: "kim",
    func: function () {
      console.log(this);
    },
  };

  obj.func();
</script>

โ—ผ Object ์•ˆ์˜ ํ•จ์ˆ˜์—์„œ this๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, Object๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

โœ” Object ์•ˆ์˜ Object ์•ˆ์˜ function์—์„œ this ์‹คํ–‰

<script>
  var obj = {
    data: {
      func: function () {
        console.log(this);
      },
    },
  };

  obj.data.func();
</script>

โ—ผ obj.data Object์˜ ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋œ๋‹ค.

 

โœ” Object ์•ˆ์˜ Arrow function์—์„œ this ์‹คํ–‰

<script>
  var obj = {
    func: () => {
      console.log(this);
    },
  };

  obj.func();
</script>

โ—ผ ์ถœ๋ ฅ ๊ฒฐ๊ณผ๋Š” window์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ ํ‘œ์‹œ๋œ๋‹ค.

โ—ผ Arrow Function์˜ ํŠน์ง•์€ ๋‚ด๋ถ€ this ๊ฐ’์„ ๋ณ€ํ™”์‹œํ‚ค์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ฐ–์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•œ๋‹ค.


๐Ÿ’ฅ ์ด๋Ÿฐ ๊ฒฐ๊ณผ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์ด์œ 

<script>
  function test() {
    console.log(this);
  }

  test(); // 1๋ฒˆ
  window.test(); // 2๋ฒˆ
</script>

JavaScript์—์„œ๋Š” ํ•จ์ˆ˜๋‚˜ ๋ณ€์ˆ˜๋ฅผ ์ „์—ญ ๊ณต๊ฐ„์—์„œ ๋งŒ๋“ค๋ฉด {window}์— ๋ณด๊ด€ํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ์ „์—ญ ๊ณต๊ฐ„์—์„œ this๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด {window} ๋‚ด์šฉ์ด ์ถœ๋ ฅ๋œ๋‹ค. ๋”ฐ๋ผ์„œ, 1๋ฒˆ ์ฝ”๋“œ๋‚˜ 2๋ฒˆ ์ฝ”๋“œ ํ˜•์‹์œผ๋กœ test๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ฆ‰, this๋Š” ๋‚˜๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” Object์˜ ์ •๋ณด๋ฅผ ์ถœ๋ ฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—,  ์ „์—ญ ๊ณต๊ฐ„์—์„œ this๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด {window}๊ฐ€ ์ถœ๋ ฅ๋˜๊ณ  obj๋ผ๋Š” Object์—์„œ this๋ฅผ ์ถœ๋ ฅํ•˜๋ฉด {obj}๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

โœ” contructor์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

<script>
  function test() {
    this.name = "hi";
  }

  var testObj = new test();
  console.log(testObj);
</script>

โ—ผ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋˜๋Š” this๋Š” ์ƒˆ๋กœ ์ƒ์„ฑ๋˜๋Š” Object์˜ instance์ด๋‹ค.

 

โœ” EventListener์— ๋“ฑ๋กํ•˜๋Š” function์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

<div>
  <button id="btn">๋ฒ„ํŠผ</button>
</div>

<script>
  document.getElementById("btn").addEventListener("click", function (e) {
    console.log(this);
  });
</script>

โ—ผ ์—ฌ๊ธฐ์„œ ์‚ฌ์šฉ๋˜๋Š” this๋Š” document.getElementById()๋กœ ์ฐพ์€ ํƒœ๊ทธ์ด๋‹ค.

โ—ผ e.currentTarget์ด๋ž‘ ๋™์ผํ•œ ์ถœ๋ ฅ ๊ฐ’์„ ๋‚˜ํƒ€๋‚ธ๋‹ค.

 

โœ” EventListener์— ๋“ฑ๋กํ•˜๋Š” Arrow function์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

<div>
  <button id="btn">๋ฒ„ํŠผ</button>
</div>

<script>
  document.getElementById("btn").addEventListener("click", () => {
    console.log(this);
  });
</script>

โ—ผ Arrow Function์˜ ํŠน์ง•์œผ๋กœ this๊ฐ€ ์žฌ์ •์˜ ๋˜์ง€ ์•Š์•„, this ๊ฐ’์ด {window}๋กœ ์ถœ๋ ฅ๋œ๋‹ค.

 

โœ” Object์•ˆ์˜ function์•ˆ์˜ Array ForEach์— ๋“ฑ๋กํ•œ function์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

<script>
  var obj = {
    func: function () {
      var arr = [1, 2, 3];
      arr.forEach(function () {
        console.log(this);
      });
    },
  };

  obj.func();
</script>

โ—ผ {window}๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค. 

โ—ผ this๋ฅผ ์ถœ๋ ฅํ•˜๋Š” function์€ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์ง€ ์•Š๊ณ , ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋กœ ๋“ฑ๋กํ•˜๋Š” function ์ด๊ธฐ์— {window}๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

 

โœ” Object์•ˆ์˜ function์•ˆ์˜ Array ForEach์— ๋“ฑ๋กํ•œ Arrow function์—์„œ this ์‚ฌ์šฉํ•˜๊ธฐ

<script>
  var obj = {
    func: function () {
      var arr = [1, 2, 3];
      arr.forEach(() => {
        console.log(this);
      });
    },
  };

  obj.func();
</script>

โ—ผ {obj}๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.

โ—ผ Arrow Function์€ this๋ฅผ ์žฌ์ •์˜ ํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ๋ฐ–์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์—, func์˜ this ์ถœ๋ ฅ ๊ฐ’์ด ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅ๋œ๋‹ค.


๐Ÿ’ฅ function๊ณผ Arrow function ์–ธ์ œ ์‚ฌ์šฉํ• ๊นŒ?

โœ” function ์‚ฌ์šฉ ์˜ˆ์ œ

<script>
  var obj = {
    name: "kim",
    say: function () {
      console.log(`๋‚˜๋Š” ${this.name}์ž…๋‹ˆ๋‹ค.`);
    },
  };

  obj.say();
</script>

โ—ผ this ๊ฐ’์„ ์žฌ์ •์˜ํ•˜์—ฌ ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ, function์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

  โ— ์ƒ์œ„ ์˜ค๋ธŒ์ ํŠธ์˜ ๊ฐ’์œผ๋กœ ์žฌ์ •์˜ํ•ด์„œ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

 

โœ” Array function ์‚ฌ์šฉ ์˜ˆ์ œ

<button id="btn">๋ฒ„ํŠผ</button>

<script>
  document.getElementById("btn").addEventListener("click", function () {
    setTimeout(() => {
      console.log(this.innerHTML);
    }, 1000);
  });
</script>

โ—ผ this ๊ฐ’์„ ์žฌ์ •์˜ํ•˜์ง€ ์•Š๊ณ  ์‚ฌ์šฉํ•ด์•ผ ํ•  ๋•Œ, Arrow function์„ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ์ž‘์„ฑํ•œ๋‹ค.

  โ— ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ์จ ์‚ฌ์šฉ๋˜์–ด, ์ƒ์œ„ Object ๊ฐ’์„ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ

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

JavaScript ES6 & ๊ฐ์ฒด ์ง€ํ–ฅ ๋ฌธ๋ฒ•  (1) 2023.04.25
JavaScript ๊ธฐ๋ณธ ๋ฐ ์‚ฌ์šฉํ•˜๊ธฐ  (0) 2023.04.23