๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป
article thumbnail
๋ฐ˜์‘ํ˜•

[JavaScript] this๊ฐ€ ๋ฌด์—‡์ผ๊นŒ์š”? ์ฝ”๋“œ ์˜ˆ์ œ๋กœ ์‰ฝ๊ฒŒ ์•Œ์•„๋ด…์‹œ๋‹ค.

this ํ‚ค์›Œ๋“œ์˜ ์ •์˜

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ this๋Š” ํ˜„์žฌ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ํŠน๋ณ„ํ•œ ํ‚ค์›Œ๋“œ์ž…๋‹ˆ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ ์‹คํ–‰ ์ปจํ…์ŠคํŠธ๊ฐ€ ์ƒ์„ฑ๋  ๋•Œ ํ•จ๊ป˜ ๊ฒฐ์ •์ด ๋˜๋Š”๋ฐ์š”.

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

์ผ๋ฐ˜ ํ•จ์ˆ˜์—์„œ์˜ this

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

์ „์—ญ ๊ฐ์ฒด๋ž€?
๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ๋Š” window,
Node.js ํ™˜๊ฒฝ์—์„œ๋Š” global ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.

window๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์†์„ฑ๋“ค

 

function main() {
  console.log(this);
}
main(); // window ๊ฐ์ฒด ๋˜๋Š” global ๊ฐ์ฒด ์ถœ๋ ฅ

์œ„์™€ ๊ฐ™์ด ์ด๋ ‡๊ฒŒ ์ „์—ญ์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ์€

function main() {
  console.log(this);
}
windoow.main(); // window ๊ฐ์ฒด ์ถœ๋ ฅ

๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ ๊ธฐ์ค€์œผ๋กœ window ๊ฐ์ฒด์˜ main ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

(์ „์—ญ์—์„œ์˜ ํ˜ธ์ถœ์€ window๋ฅผ ์ƒ๋žตํ•œ ๊ฒƒ๊ณผ ๊ฐ™๋‹ค๊ณ  ๋ด๋„ ๋ฌด๋ฐฉํ•ฉ๋‹ˆ๋‹ค.)

 

์œ„์—์„œ this๋Š” 'ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐ' ํ•œ๋‹ค๊ณ  ์„ค๋ช…ํ–ˆ์Šต๋‹ˆ๋‹ค.

main ํ•จ์ˆ˜๋ฅผ ์ง์ ‘์ ์œผ๋กœ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” window ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— main ํ•จ์ˆ˜์˜ this๋Š” window ๊ฐ์ฒด๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.


๋‹ค๋งŒ 'use strict' ๋ชจ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ์ „์—ญ ๊ณต๊ฐ„์—์„œ window ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜์˜ this๋Š” undefined๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ์—์„œ์˜ this

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

const obj = {
  name: 'yong',
  sayHello: function () {
    console.log(this);
  },
};
obj.sayHello(); // {name: 'yong', sayHello: ƒ} ์ถœ๋ ฅ

์œ„ ์ฝ”๋“œ๋ฅผ ๋ณด๋ฉด obj๋ผ๋Š” ๊ฐ์ฒด๋กœ๋ถ€ํ„ฐ sayHello ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ–ˆ์Šต๋‹ˆ๋‹ค.


const obj = {
  name: 'yong',
  sayHello: function () {
    console.log(this.name);
  },
};
const obj2 = obj.sayHello;

obj.sayHello(); // {name: 'yong', sayHello: ƒ} ์ถœ๋ ฅ
obj2(); // window ๊ฐ์ฒด ์ถœ๋ ฅ

obj2๋Š” ์™œ obj์˜ sayHello๋ฅผ ์ฐธ์กฐํ•จ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ์™œ window ๊ฐ์ฒด๊ฐ€ ์ถœ๋ ฅ๋ ๊นŒ์š”?
๋ฐ”๋กœ obj์˜ ํ˜ธ์ถœ ์ฃผ์ฒด๋Š” window๊ฐ€ ๋˜๊ธฐ ๋•Œ๋ฌธ์ž…๋‹ˆ๋‹ค. ( window.obj2() == obj2() )


const obj = {
  name: 'yong',
  sayHello: {
    name: 'jun',
    sayBye: function () {
      console.log(this);
    },
  },
};

obj.sayHello.sayBye(); // {name: 'jun', sayBye: ƒ} ์ถœ๋ ฅ

์œ„ ์ฝ”๋“œ์—์„œ๋Š” sayBye๋ฉ”์†Œ๋“œ๋ฅผ ์ง์ ‘์ ์œผ๋กœ ์‹คํ–‰ํ•œ ๊ฑด sayHello๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— sayHello ๊ฐ์ฒด๊ฐ€ ์ถœ๋ ฅ์ด ๋˜์—ˆ์Šต๋‹ˆ๋‹ค.

์ฆ‰, ์–ด๋–ค ํ•จ์ˆ˜๋ฅผ ๋ฉ”์†Œ๋“œ๋กœ์„œ ํ˜ธ์ถœํ•˜๋Š” ๊ฒฝ์šฐ ํ˜ธ์ถœ ์ฃผ์ฒด๋Š” ๋ฐ”๋กœ ํ•จ์ˆ˜๋ช…(ํ”„๋กœํผํ‹ฐ๋ช…) ์•ž์˜ ๊ฐ์ฒด์ž…๋‹ˆ๋‹ค.

์  ํ‘œ๊ธฐ๋ฒ•์˜ ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ์ (.) ์•ž์— ๋ช…์‹œ๋œ ๊ฐ์ฒด๊ฐ€ ๊ณง this๊ฐ€ ๋˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ƒ์„ฑ์ž ํ•จ์ˆ˜์—์„œ์˜ this

new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ 'this'๋Š” ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.


์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์šฐ์„  ์ƒ์„ฑ์ž์˜ prototype ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋Š” __proto__๋ผ๋Š” ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋Š” ๊ฐ์ฑ„(์ธ์Šคํ„ด์Šค)๋ฅผ ๋งŒ๋“ค๊ณ , ๋ฏธ๋ฆฌ ์ค€๋น„๋œ ๊ณตํ†ต ์†์„ฑ ๋ฐ ๊ฐœ์„ฑ์„ ํ•ด๋‹น ๊ฐ์ฒด(this)์— ๋ถ€์—ฌํ•ฉ๋‹ˆ๋‹ค. ์ด๋ ‡๊ฒŒ ํ•ด์„œ ๊ตฌ์ฒด์ ์ธ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋งŒ๋“ค์–ด์ง‘๋‹ˆ๋‹ค.
(ํ•ด๋‹น ๋‚ด์šฉ์€ JavasScript์˜ prototype์— ๋Œ€ํ•œ ์ดํ•ด๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.)
const Human = function (name, age) {
  this.name = name;
  this.age = age;
};

const yong = new Human('yong', 3);
const jun = new Human('jun', 10);

console.log(yong); // {name: 'yong', age: 3} ์ถœ๋ ฅ
console.log(jun); // {name: 'jun', age: 10} ์ถœ๋ ฅ

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ์˜ this

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

์ฆ‰, ์ž์‹ ๋งŒ์˜ this๊ฐ€ ์—†๊ณ  ์ž์‹ ์„ ๊ฐ์‹ธ๊ณ  ์žˆ๋Š” scope๋กœ ์˜ฌ๋ผ๊ฐ€์„œ this๋ฅผ ์ฐพ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

// innerFunction ์ผ๋ฐ˜ ํ•จ์ˆ˜
const obj = {
  name: 'yong',
  main: function () {
    const innerFunction = function () {
      console.log(this);
    };
    innerFunction();
  },
};

obj.main(); // Window ๊ฐ์ฒด ์ถœ๋ ฅ

// innerFunction ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
const obj = {
  name: 'yong',
  main: function () {
    const innerFunction = () => {
      console.log(this);
    };
    innerFunction();
  },
};

obj.main(); // {name: 'yong', main: ƒ} ์ถœ๋ ฅ

์™œ ๊ฒฐ๊ณผ๊ฐ€ ๋‹ค๋ฅธ์ง€ ๋ณผ๊นŒ์š”?


๋จผ์ € innerFunction ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋จผ์ € ์‚ดํŽด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

innerFunction ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ์ฃผ์ฒด๋Š” window์ด๊ธฐ ๋•Œ๋ฌธ์— window ๊ฐ์ฒด๊ฐ€ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ innerFunction ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ฅผ ์‚ดํŽด๋ณด๋ฉด,

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ this๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•œ๋‹ค๊ณ  ํ–ˆ์Šต๋‹ˆ๋‹ค.

๊ทธ๋ž˜์„œ, ์ƒ์œ„ ์Šค์ฝ”ํ”„์ธ main ๋ฉ”์„œ๋“œ์˜ this๋Š” obj๊ฐ€ ํ˜ธ์ถœ์„ ํ–ˆ๊ธฐ ๋•Œ๋ฌธ์— main ๋ฉ”์†Œ๋“œ์˜ this๋Š” obj ๊ฐ์ฒด๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.

์ฆ‰, innerFunction์€ main ๋ฉ”์†Œ๋“œ์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉํ•ด์„œ this๋Š” obj ๊ฐ์ฒด๊ฐ€ ๋˜์–ด์„œ obj ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

 

์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์˜ this์— ๋Œ€ํ•œ ์ฐจ์ด๋ฅผ ์ •๋ฆฌํ•ด ๋ณด๋ฉด ์•„๋ž˜์™€ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜: ์„ ์–ธ๋œ ์‹œ์ ์—์„œ ๊ฒฐ์ •์ด ๋ฉ๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜: ์–ด๋””์„œ ์„ ์–ธ์ด ๋˜์—ˆ๋Š”์ง€ ์ƒ๊ด€์—†์ด ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋œ ๋ฐฉ๋ฒ•์— ๋”ฐ๋ผ์„œ this๊ฐ€ ๋™์ ์œผ๋กœ ํ• ๋‹น๋ฉ๋‹ˆ๋‹ค.

๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•˜๋Š” ๋ฐฉ๋ฒ• (call/apply/bind)

์ƒํ™ฉ๋งˆ๋‹ค ๋‹ฌ๋ผ์ง€๋Š” this์— ๋Œ€ํ•ด์„œ ๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ์ง€์ •ํ•ด์ค„ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

call ๋ฉ”์„œ๋“œ

call ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ์˜ ํ˜ธ์ถœ ์ฃผ์ฒด์ธ ํ•จ์ˆ˜๋ฅผ this ๊ฐ’ ๋ฐ ๊ฐ๊ฐ ์ „๋‹ฌ๋œ ์ธ์ˆ˜์™€ ํ•จ๊ป˜ ํ•จ์ˆ˜๋ฅผ ์ฆ‰์‹œ ์‹คํ–‰ํ•˜๋„๋ก ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ž…๋‹ˆ๋‹ค.

// ํ•จ์ˆ˜
const func = function (a, b, c) {
  console.log(this, a, b, c);
};

func(1, 2, 3); // Window 1 2 3๊ฐ์ฒด ์ถœ๋ ฅ
func.call({ x: 1 }, 4, 5, 6); // {x: 1} 4 5 6 ์ถœ๋ ฅ

// ๋ฉ”์†Œ๋“œ
const obj = {
  a:1,
  method: function (x,y) {
    console.log(this,x,y);
    
  } 
}

obj.method(2,3); // {a: 1, method: ƒ} 2 3 ์ถœ๋ ฅ
obj.method.call({z:1},4,5); // {z: 1} 4 5 ์ถœ๋ ฅ

call ๋ฉ”์„œ๋“œ์˜ ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ๋Š” this๋กœ ๋ฐ”์ธ๋”ฉ ํ•  ๊ฐ’์„ ์ „๋‹ฌํ•ด์ฃผ์–ด์„œ this๋ฅผ ๋ฐ”์ธ๋”ฉํ•ฉ๋‹ˆ๋‹ค.

์ดํ›„์˜ ์ธ์ž๋“ค์€ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœํ•ฉ๋‹ˆ๋‹ค.

apply ๋ฉ”์†Œ๋“œ

apply ๋ฉ”์„œ๋“œ๋Š” call ๋ฉ”์„œ๋“œ์™€ ๊ธฐ๋Šฅ์ ์œผ๋กœ ๋งค์šฐ ๋™์ผํ•ฉ๋‹ˆ๋‹ค.

call ๋ฉ”์†Œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์ธ์ž๋“ค์„ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•˜๋Š” ๋ฐ˜๋ฉด,

apply ๋ฉ”์†Œ๋“œ๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›์•„ ๊ทธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •ํ•ฉ๋‹ˆ๋‹ค.

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

func(1, 2, 3); // Window 1 2 3์ถœ๋ ฅ
func.apply({ x: 1 }, [4, 5, 6]); // {x: 1} 4 5 6 ์ถœ๋ ฅ

const obj = {
  a: 1,
  method: function (x, y) {
    console.log(this, x, y);
  },
};

obj.method(2, 3); // {a: 1, method: ƒ} 2 3 ์ถœ๋ ฅ
obj.method.apply({ z: 1 }, [4, 5]); // {z: 1} 4 5 ์ถœ๋ ฅ

bind ๋ฉ”์†Œ๋“œ

bind ๋ฉ”์†Œ๋“œ๋Š” ES5์— ์ถ”๊ฐ€๋œ ๊ธฐ๋Šฅ์ž…๋‹ˆ๋‹ค.

bind๋Š” call/apply ๋ฉ”์†Œ๋“œ์™€ ๋‹ค๋ฅด๊ฒŒ ์ฆ‰์‹œ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  ๋„˜๊ฒจ๋ฐ›์€ this ๋ฐ ์ธ์ˆ˜๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

const main = function () {
  console.log(this);
};
const mainBind = main.bind({ name: 'mainBind' });

main(); // Window ๊ฐ์ฒด ์ถœ๋ ฅ
mainBind(); // {name: 'mainBind'} ์ถœ๋ ฅ

์ฃผ์˜ํ•  ์ ์€ ์ด๋ฏธ bind ๋œ ๊ฒƒ์— ๋‹ค์‹œ bind๋ฅผ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์ฆ‰, ์ตœ์ดˆ๋กœ bind ํ•œ ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋งŒ ์ ์šฉ์ด ๋ฉ๋‹ˆ๋‹ค.

const main = function () {
  console.log(this);
};
const mainBind = main.bind({ name: 'mainBind' });
const mainBindBind = mainBind.bind({ name: 'mainBindBind' });

main(); // Window ๊ฐ์ฒด ์ถœ๋ ฅ
mainBind(); // {name: 'mainBind'}
mainBindBind(); // {name: 'mainBind'}

๋˜ ๋‹ค๋ฅธ ์ฃผ์˜ํ•  ์ ์œผ๋กœ๋Š” ํ™”์‚ดํ‘œ ํ•จ์ˆ˜์—์„œ๋Š” bind๊ฐ€ ์ ์šฉ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” bind๋ฅผ ํ•ด ์ค„ ์ž์‹ ๋งŒ์˜ this๋ฅผ ๊ฐ–๊ณ  ์žˆ์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— bind๋ฅผ ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.

์„ ์–ธ๋˜๋Š” ์‹œ์ ์—์„œ ๊ฒฐ์ •์ด ๋˜๊ณ  ์ดํ›„์—๋Š” ๋ณ€๊ฒฝ๋˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— bind๋ฅผ ํ•ด ์ค„ ์˜๋ฏธ๊ฐ€ ์—†๊ณ  bind๋ฅผ ํ•œ๋‹ค๊ณ  ํ•˜๋”๋ผ๋„ ๋ฌด์‹œ๋ฅผ ํ•ด๋ฒ„๋ฆฝ๋‹ˆ๋‹ค.

const obj = {
  name: 'yong',
  main: function () {
    const innerFunction = (() => {
      console.log(this);
    }).bind({name:'bind'});
    innerFunction();
  },
};

obj.main(); // {name: 'yong', main: ƒ}

const greet = () => {
  console.log(`Hello, ${this.name}`);
};
const boundGreet = greet.bind({ name: 'yong' });

boundGreet(); // 'Hello, undefined'

 

โœ… ์ •๋ฆฌ

  • this์˜ ๊ฐ’์€ ํ•จ์ˆ˜๊ฐ€ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœ๋˜์—ˆ๋Š”์ง€์— ๋”ฐ๋ผ ๋ณ€ํ•˜๋ฉฐ, ์ผ๋ฐ˜์ ์œผ๋กœ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ•œ๋‹ค.
  • ์—„๊ฒฉ ๋ชจ๋“œ์—์„œ๋Š” ์ผ๋ฐ˜ ํ•จ์ˆ˜์˜ this๊ฐ€ 'undefined'์ž…๋‹ˆ๋‹ค.
  • ์ผ๋ฐ˜ํ•จ์ˆ˜์—์„œ this๋Š” '์ „์—ญ ๊ฐ์ฒด'๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
  • ๊ฐ์ฒด์˜ ๋ฉ”์†Œ๋“œ์—์„œ this๋Š” ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
  • new ํ‚ค์›Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ 'this'๋Š” ์ƒˆ๋กœ ์ƒ์„ฑ๋œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋Š” ์ƒ์œ„ ์Šค์ฝ”ํ”„์˜ this๋ฅผ ๊ทธ๋Œ€๋กœ ํ™œ์šฉ
  • ์ผ๋ฐ˜ ํ•จ์ˆ˜: ์‹คํ–‰ ์‹œ์ ์—์„œ this๊ฐ€ ๊ฒฐ์ •๋จ
  • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜: ์„ ์–ธ ์‹œ์ ์—์„œ this๊ฐ€ ๊ฒฐ์ •๋จ
  • call๊ณผ 'apply ๋ฉ”์†Œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋กœ ๋ฐ›์€ ํŠน์ • ๊ฐ์ฒด๋ฅผ 'this'๋กœ ์„ค์ •ํ•˜๊ณ , ๋‚˜๋จธ์ง€ ์ธ์ž๋“ค์„ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
  • call ๋ฉ”์„œ๋“œ๋Š” ์ฒซ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ์ œ์™ธํ•œ ๋ชจ๋“  ์ธ์ž๋“ค์„ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •
  • apply ๋ฉ”์†Œ๋“œ๋Š” ๋‘ ๋ฒˆ์งธ ์ธ์ž๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›์•„ ๊ทธ ๋ฐฐ์—ด์˜ ์š”์†Œ๋“ค์„ ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์ง€์ •
  • bind๋Š” ๋„˜๊ฒจ๋ฐ›์€ this ๋ฐ ์ธ์ˆ˜๋“ค์„ ๋ฐ”ํƒ•์œผ๋กœ ์ƒˆ๋กœ์šด ํ•จ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜

์ฐธ๊ณ  ๋ฌธํ—Œ:

๋ฐ˜์‘ํ˜•
profile

๐Ÿ’ป์šฉ๋‡ฝ ๊ฐœ๋ฐœ ๋…ธํŠธ๐Ÿ’ป

@์šฉ๋‡ฝ

ํฌ์ŠคํŒ…์ด ์ข‹์•˜๋‹ค๋ฉด "์ข‹์•„์š”โค๏ธ" ๋˜๋Š” "๊ตฌ๋…๐Ÿ‘๐Ÿป" ํ•ด์ฃผ์„ธ์š”!