๐Ÿ“ŒLanguage/JavaScript

[Deep Dive] this

hellohailie 2022. 12. 7. 23:14

โœ”๏ธ ์ด๋ฒˆ ์ •๋…์„ ํ†ตํ•ด ์•Œ๊ฒŒ ๋œ ๊ฒƒ
this๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?
this ๋ฐ”์ธ๋”ฉ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”? 

 

<๋‹ต ๋ฏธ๋ฆฌ๋ณด๊ธฐ>

๐Ÿค” this๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”?

๐Ÿค“ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜์ž…๋‹ˆ๋‹ค. 

 

๐Ÿค” this ๋ฐ”์ธ๋”ฉ์ด๋ž€ ๋ฌด์—‡์ธ๊ฐ€์š”? 

๐Ÿค“ this์™€ this๊ฐ€ ๊ฐ€๋ฆฌํ‚ฌ ๊ฐ์ฒด๋ฅผ ์—ฐ๊ฒฐํ•˜๋Š” ๊ณผ์ •์ž…๋‹ˆ๋‹ค. 

 


โœ”๏ธ this๋ž€?

์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๋‹ค. 

๊ฐ์ฒด์—์„œ ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ ์ด ์†ํ•œ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ๋Ÿฌ๊ธฐ ์œ„ํ•ด์„œ๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

์ด ์—ญํ• ์„ this๊ฐ€ ํ•œ๋‹ค. 

 

this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

 


โœ”๏ธ this์˜ ํŠน์ง•

 

โ—† this๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์— ์˜ํ•ด ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ, ์ฝ”๋“œ ์–ด๋””์„œ๋“  ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. 

โ—† this๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๊ฐ’ (๋ฐ”์ธ๋”ฉ)์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹์— ์˜ํ•ด ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค. 

this ๋ฐ”์ธ๋”ฉ์ด๋ž€?
๋ฐ”์ธ๋”ฉ์ด๋ž€ ์‹๋ณ„์ž์™€ ๊ฐ’์„ ์—ฐ๊ฒฐํ•˜๋Š” ๊ณผ์ •์„ ์˜๋ฏธํ•œ๋‹ค. 
(๋ณ€์ˆ˜ ์„ ์–ธ์€ ๋ณ€์ˆ˜ ์ด๋ฆ„(์‹๋ณ„์ž)๊ณผ ํ™•๋ณด๋œ ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์˜ ์ฃผ์†Œ๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. )
this ๋ฐ”์ธ๋”ฉ์€ this์™€ this๊ฐ€ ๊ฐ€๋ฆฌํ‚ฌ ๊ฐ์ฒด๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค. 

 

โ—† this๋Š” ์ฝ”๋“œ ์–ด๋””์—์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค. (์ „์—ญ, ์ง€์—ญ ๋ชจ๋‘ ๊ฐ€๋Šฅ)

// this๋Š” ์–ด๋””์„œ๋“ ์ง€ ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค.
// ์ „์—ญ์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
console.log(this); // window

function square(number) {
  // ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ „์—ญ ๊ฐ์ฒด window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  console.log(this); // window
  return number * number;
}
square(2);

const person = {
  name: 'Lee',
  getName() {
    // ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    console.log(this); // {name: "Lee", getName: ƒ}
    return this.name;
  }
};
console.log(person.getName()); // Lee

function Person(name) {
  this.name = name;
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  console.log(this); // Person {name: "Lee"}
}

const me = new Person('Lee');

 

โ—† this๋Š” ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€ ๋˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. 

 

 


โœ”๏ธ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹๊ณผ this ๋ฐ”์ธ๋”ฉ

this ๋ฐ”์ธ๋”ฉ์€ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹, ์ฆ‰ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค. 

1. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ

๐Ÿ‘‰ ๊ธฐ๋ณธ์ ์œผ๋กœ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด(window)๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 

๋‹ค๋งŒ this๋Š” ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜์ง€ ์•Š๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ this๋Š” ์˜๋ฏธ๊ฐ€ ์—†๋‹ค. 

 

function foo() {
  'use strict';

  console.log("foo's this: ", this);  // undefined
  function bar() {
    console.log("bar's this: ", this); // undefined
  }
  bar();
}
foo();

๐Ÿ‘‰ strict mode๊ฐ€ ์ ์šฉ๋˜๋ฉด this์—๋Š” undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 

 

// var ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ์ „์—ญ ๋ณ€์ˆ˜ value๋Š” ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‹ค.
var value = 1;
// const ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ์ „์—ญ ๋ณ€์ˆ˜ value๋Š” ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.
// const value = 1;

const obj = {
  value: 100,
  foo() {
    console.log("foo's this: ", this);  // {value: 100, foo: ƒ}
    console.log("foo's this.value: ", this.value); // 100

    // ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜
    function bar() {
      console.log("bar's this: ", this); // window
      console.log("bar's this.value: ", this.value); // 1
    }

    // ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜๋„ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
    bar();
  }
};

obj.foo();

๐Ÿ‘‰ ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜๋„ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 

 

๐Ÿ‘‰ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋„ ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 

 

๐Ÿ‘‰ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ this ๋ฐ”์ธ๋”ฉ์„ ์ผ์น˜์‹œํ‚ฌ ์ˆ˜๋„ ์žˆ๋‹ค.

var value = 1;

const obj = {
  value: 100,
  foo() {
    // ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    setTimeout(() => console.log(this.value), 100); // 100
  }
};

obj.foo();

2. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

๐Ÿ‘‰ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 

 


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

๐Ÿ‘‰ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ๋ฏธ๋ž˜์— ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. 

// ์ƒ์„ฑ์ž ํ•จ์ˆ˜
function Circle(radius) {
  // ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  this.radius = radius;
  this.getDiameter = function () {
    return 2 * this.radius;
  };
}

// ๋ฐ˜์ง€๋ฆ„์ด 5์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const circle1 = new Circle(5);
// ๋ฐ˜์ง€๋ฆ„์ด 10์ธ Circle ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
const circle2 = new Circle(10);

console.log(circle1.getDiameter()); // 10
console.log(circle2.getDiameter()); // 20

 

๋‹จ, new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด๋‹ˆ๊นŒ

// new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด๋‹ค.
const circle3 = Circle(15);

// ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Circle์—๋Š” ๋ฐ˜ํ™˜๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ ์•”๋ฌต์ ์œผ๋กœ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
console.log(circle3); // undefined

// ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Circle ๋‚ด๋ถ€์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
console.log(radius); // 15

 


4. Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ => ์•„์ง ์ดํ•ด ์•ˆ๋จ...... ์ถ”๊ฐ€ ๊ณต๋ถ€ ํ•„์š”....

๐Ÿ‘‰ Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด

 

 

๐Ÿ‘ฉ‍๐Ÿ’ป ๋ชจ๋˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ Deep Dive๋ฅผ ์ •๋…ํ•˜๊ณ  ์ œ๊ฐ€ ์ดํ•ดํ•œ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•œ ๊ธ€์ž…๋‹ˆ๋‹ค ๐Ÿ‘ฉ‍๐Ÿ’ป