์ฝ”์–ดJS ์ •๋ฆฌ ๐Ÿ“– | this

hyebin Joยท2022๋…„ 4์›” 8์ผ
0

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ this๊ฐ€ ๋ฐ”๋ผ๋ณด๋Š” ๋Œ€์ƒ์€ ์‹คํ–‰์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ, ์ฆ‰ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ํ˜ธ์ถœ ๋ฐฉ์‹์— ๋”ฐ๋ผ ๊ฒฐ์ •๋ฉ๋‹ˆ๋‹ค.

์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ฌ๋ผ์ง€๋Š” this

1. ์ „์—ญ๊ณต๊ฐ„์—์„œ์˜ this

๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ ์ „์—ญ๊ฐ์ฒด๋Š” window
Node.js ํ™˜๊ฒฝ์—์„œ ์ „์—ญ๊ฐ์ฒด๋Š” global

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

๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์‹œ, ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

const obj = {
  a: 2,
  method1: function () {console.log(this)},
  method2: function () {console.log(this.a)},
  innerObj: {
    method3: function () {
      console.log(this);
    },
  },
};

obj.method1(); //obj
obj['method1'](); //obj

obj.method2(); //2
obj['method2'](); //2

obj.innerObj.method3(); //obj.innerObj
obj['innerObj']['method3'](); //obj.innerObj

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

์ผ๋ฐ˜ํ•จ์ˆ˜ ํ˜ธ์ถœ์‹œ, ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this๋Š” ์ „์—ญ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

const func = function() {console.log(this)};
func(); //์ „์—ญ๊ฐ์ฒด

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

const obj = {
  innerFunc : function() {
    const self = this;
    console.log(self);
  },
}

obj.innerFunc(); //obj

ํ™”์‚ดํ‘œํ•จ์ˆ˜๋Š” ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ๋•Œ, this๋ฐ”์ธ๋”ฉ์ด ๋˜์ง€ ์•Š๊ณ  ์ƒ์œ„์Šค์ฝ”ํ”„์˜ this๋ฅผ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const obj = {
  outer: function() {
    console.log(this); //obj
    const innerFunc = () => {
      console.log(this);
    };
    innerFunc(); //obj (ํ™”์‚ดํ‘œํ•จ์ˆ˜์ด๋ฏ€๋กœ this๋Š” ์ƒ์œ„์Šคํฌํฌ์ธ obj)
  }
};

obj.outer();

this ๋ฐ”์ธ๋”ฉ

1. call ๋ฉ”์„œ๋“œ

๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ์ฃผ์ฒด์ธ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ๋ช…๋ น์–ด์ž…๋‹ˆ๋‹ค.

const func = function(a,b,c) {
  console.log(this, a,b,c);
};

func.call({x:1},2,3,4) // {x:1} 2 3 4
//call ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ func๊ฐ€ ๋ฐ”๋กœ ์‹คํ–‰๋˜๊ณ 
//funcํ•จ์ˆ˜ ๋‚ด์˜ this๋Š” {x:1}์— ๋ฐ”์ธ๋”ฉ

2. apply ๋ฉ”์„œ๋“œ

call๋ฉ”์„œ๋“œ์™€ ๊ธฐ๋Šฅ์ ์œผ๋กœ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.
๋‹ค๋งŒ apply ๋ฉ”์„œ๋“œ๋Š” ๋‘๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

const func = function(a,b,c) {
  console.log(this, a,b,c);
};

func.apply({x:1},[2,3,4]) // {x:1} 2 3 4

3. bind ๋ฉ”์„œ๋“œ

call, apply ๊ธฐ๋Šฅ๊ณผ ๋น„์Šทํ•˜์ง€๋งŒ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ํ˜ธ์ถœํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์€ thisArg ๋ฐ ์ธ์ˆ˜๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

const func = function(a,b,c) {
  console.log(this, a,b,c);
 };

const bindFunc1 = func.bind({x:1}); //this๋ฅผ {x:1}์— ๋ฐ”์ธ๋”ฉํ•œ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
bindFunc1(2,3,4); // {x:1} 2 3 4

const bindFunc2 = func.bind({x:1},7,8); //this๋ฅผ {x:1}์— ๋ฐ”์ธ๋”ฉ.  ํ•˜๊ณ  ์•ž์—์„œ๋ถ€ํ„ฐ ๋‘๊ฐœ์˜ ์ธ์ˆ˜๋ฅผ 7,8๋กœ ์ง€์ •ํ•œ ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
bindFunc2(9); //{x:1} 7 8 9

4. ์ฝœ๋ฐฑํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ์˜ this

์ฝœ๋ฐฑํ•จ์ˆ˜๋„ ํ•จ์ˆ˜์ด๊ธฐ ๋•Œ๋ฌธ์— this๋Š” ์ „์—ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•ฉ๋‹ˆ๋‹ค.

 [3,4,5].forEach(function(x){console.log(this,x)});
//์ „์—ญ๊ฐ์ฒด 3
//์ „์—ญ๊ฐ์ฒด 4
//์ „์—ญ๊ฐ์ฒด 5 

const obj = {
  arr: function() {
    [3,4,5].forEach(function(x) {console.log(this,x)});
  }
}
obj.arr();
//์ „์—ญ๊ฐ์ฒด 3
//์ „์—ญ๊ฐ์ฒด 4
//์ „์—ญ๊ฐ์ฒด 5 

์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์ธ์ž๋กœ ๋ฐ›๋Š” ๋ฉ”์„œ๋“œ ์ค‘ ์ผ๋ถ€๋Š” this๋ฅผ ๋ณ„๋„์˜ ์ธ์ž(thisArg)๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
thisArg๋ฅผ ๋ฐ›๋Š” ๊ฒฝ์šฐ ์˜ˆ์‹œ - forEach๋ฉ”์„œ๋“œ

const obj = {
  arr: function() {
    [3,4,5].forEach(function(x) {console.log(this,x)},this);
  }
}
obj.arr();
//obj 3
//obj 4
//obj 5

0๊ฐœ์˜ ๋Œ“๊ธ€