Skip to content

Latest commit

ย 

History

History
509 lines (383 loc) ยท 21.8 KB

this.md

File metadata and controls

509 lines (383 loc) ยท 21.8 KB

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ this

์ž‘์„ฑ์ž : ์ด์Šฌ๊ธฐ


1. this๋ž€?

  • ์—ฌํƒœ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๊ณต๋ถ€ํ•˜๋ฉด์„œ, this๊ฐ€ ๊ฐ„๊ฐ„ํžˆ ๋“ฑ์žฅํ•˜๋Š” ๊ฒƒ์„ ๋ณด์•˜์„ ๊ฒƒ์ด๋‹ค. ์—ฌ๊ธฐ์„œ๋„ ์“ฐ์ด๋Š” ๊ฒƒ ๊ฐ™๊ณ , ์ €๊ธฐ์„œ๋„ ์“ฐ์ด๋Š” ๊ฒƒ ๊ฐ™์€ this.... ? ์ •ํ™•ํžˆ ์ •์˜ํ•ด๋ณด์ž.

1.1 this์˜ ์ •์˜

  • this๋ž€ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜๋‹ค. this๋ฅผ ํ†ตํ•ด ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ์ •์˜๋งŒ ์ฝ์œผ๋ฉด ์™œ ์“ฐ์ด๋Š”์ง€, ์–ด๋–ค ๋•Œ ์“ฐ์ด๋Š” ์ง€ ์•Œ๊ธฐ ํž˜๋“œ๋‹ˆ ์ฒœ์ฒœํžˆ ์ดํ•ดํ•ด๋ณด์ž.

1.2 this๋ฅผ ์“ฐ๋Š” ์ด์œ 

  • ๊ฐ์ฒด๋Š” ์ƒํƒœ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ํ”„๋กœํผํ‹ฐ์™€ ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ•˜๋‚˜์˜ ๋…ผ๋ฆฌ์ ์ธ ๋‹จ์œ„๋กœ ๋ฌถ์€ ๋ณตํ•ฉ์ ์ธ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค. ์ด๋•Œ, ๋™์ž‘์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฉ”์„œ๋“œ๋Š” ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ์ƒํƒœ, ์ฆ‰ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์ด ๋•Œ ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋ฉด

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

  • ๋‹ค์Œ ์˜ˆ์ œ ์ฝ”๋“œ๋ฅผ ๋ณด์ž.

    const home = {
      //ํ”„๋กœํผํ‹ฐ: ๊ฐ์ฒด ๊ณ ์œ ์˜ ์ƒํƒœ ๋ฐ์ดํ„ฐ
      mom: "์Šฌ๊ธฐ",
      //๋ฉ”์„œ๋“œ: ์ƒํƒœ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์กฐ์ž‘ํ•˜๋Š” ๋™์ž‘
      getSon() {
        //์ด ๋ฉ”์„œ๋“œ๊ฐ€ ์ž์‹ ์ด ์†ํ•œ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋‹ค๋ฅธ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋ ค๋ฉด
        //์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด์ธ mom์„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค.
        return home.mom + "์™€ ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด";
      },
    };
    
    console.log(home.getSon()); //์Šฌ๊ธฐ์™€ ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด
    • getSon ๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ๋ฉ”์„œ๋“œ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž mom์„ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋‹ค. ์ด ์ฐธ์กฐ ํ‘œํ˜„์‹์ด ํ‰๊ฐ€๋˜๋Š” ์‹œ์ ์€ getSon๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜์–ด ํ•จ์ˆ˜ ๋ชธ์ฒด๊ฐ€ ์‹คํ–‰๋˜๋Š” ์‹œ์ ์ด๋‹ค.
    • ์œ„ ์˜ˆ์ œ์˜ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์€ home ๋ณ€์ˆ˜์— ํ• ๋‹น๋˜๊ธฐ ์ง์ „์— ํ‰๊ฐ€๋œ๋‹ค. ๋”ฐ๋ผ์„œ mom ๋ฉ”์„œ๋“œ๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ์‹œ์ ์—๋Š” ์ด๋ฏธ ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ํ‰๊ฐ€๊ฐ€ ์™„๋ฃŒ๋˜์–ด ๊ฐ์ฒด๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ๊ณ  home ์‹๋ณ„์ž์— ์ƒ์„ฑ๋œ ๊ฐ์ฒด๊ฐ€ ํ• ๋‹น๋œ ์ดํ›„๋‹ค. ๋”ฐ๋ผ์„œ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ home ์‹๋ณ„์ž๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

      ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ์œ ์—ฐํ•จ๊ณผ ๊ฐ•๋ ฅํ•จ์„ ๋Œ€ํ‘œํ•˜๋Š” ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹. ํด๋ž˜์Šค๋ฅผ ์ •์˜ํ•˜๊ฑฐ๋‚˜ new ์—ฐ์‚ฐ์ž๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋”๋ผ๋„ ์†์‰ฝ๊ฒŒ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ„๋‹จํžˆ ์ดํ•ดํ•˜์ž๋ฉด const x = {y:1} ์ผ ๋•Œ ์ค‘๊ด„ํ˜ธ ์•ˆ์„ ๋œปํ•œ๋‹ค.

    • ํ•˜์ง€๋งŒ! ์ž๊ธฐ ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด๋ฅผ ์žฌ๊ท€์ ์œผ๋กœ ์ฐธ์กฐํ•˜๋Š” ๋ฐฉ์‹์€ ์ผ๋ฐ˜์ ์ด์ง€ ์•Š๊ณ , ๋ฐ”๋žŒ์งํ•˜์ง€๋„ ์•Š๋‹ค.
  • ์ด๋ฒˆ์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋ฐฉ์‹์œผ๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์‚ดํŽด๋ณด์ž.

    function Home (mom){
        //์ด ์‹œ์ ์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
        ???.mom = mom;
    }
    
    Home.prototype.homeMember = function (){
        //์ด ์‹œ์ ์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
        return "๊ท€์—ฌ์šด ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด์™€ ์„ฑ์‹คํ•œ " + ???.mom;
    }
    
    //์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋จผ์ € ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ด์•ผ ํ•œ๋‹ค.
    const home = new Home("์Šฌ๊ธฐ");
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋Š” ํ”„๋กœํผํ‹ฐ ๋˜๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ์ฐธ์กฐํ•ด์•ผ ํ•œ๋‹ค. ํ•˜์ง€๋งŒ ์ƒ์„ฑ์ž ํ•จ์ˆ˜์— ์˜ํ•œ ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹์€ ๋จผ์ € ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•œ ์ดํ›„ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ๋‹จ๊ณ„๊ฐ€ ์ถ”๊ฐ€์ ์œผ๋กœ ํ•„์š”ํ•˜๋‹ค.
    • ์ฆ‰, ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๋ ค๋ฉด ๋จผ์ € ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•ด์•ผ ํ•œ๋‹ค.
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๋Š” ์‹œ์ ์—๋Š” ์•„์ง ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ธฐ ์ด์ „์ด๋ฏ€๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์‹๋ณ„์ž๋ฅผ ์•Œ ์ˆ˜ ์—†๋‹ค.
    • ๊ทธ๋ ‡๊ธฐ์— ์ž์‹ ์ด ์†ํ•œ ๊ฐ์ฒด ๋˜๋Š” ์ž์‹ ์ด ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํŠน์ˆ˜ํ•œ ์‹๋ณ„์ž๊ฐ€ ํ•„์š”ํ•˜๋‹ค. ์ด๋ฅผ ์œ„ํ•ด ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” this๋ผ๋Š” ํŠน์ˆ˜ํ•œ ์‹๋ณ„์ž๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • this๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์—”์ง„์— ์˜ํ•ด ์•”๋ฌต์ ์œผ๋กœ ์ƒ์„ฑ๋˜๋ฉฐ, ์ฝ”๋“œ ์–ด๋””์„œ๋“  ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด argument ๊ฐ์ฒด์™€ this๊ฐ€ ์•”๋ฌต์ ์œผ๋กœ ํ•จ์ˆ˜ ๋‚ด๋ถ€์— ์ „๋‹ฌ๋œ๋‹ค. ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ argument๋ฅผ ์ง€์—ญ๋ณ€์ˆ˜์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ฒ˜๋Ÿผ this๋„ ์ง€์—ญ ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

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

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

  • ์ฒซ๋ฒˆ ์งธ ์˜ˆ์ œ์— this๋ฅผ ์‚ฌ์šฉํ•ด ์ˆ˜์ •ํ•ด ๋ณด์•˜๋‹ค.

    const home = {
      mom: "์Šฌ๊ธฐ",
      getSon() {
        //this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
        return this.mom + "์™€ ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด";
      },
    };
    
    console.log(home.getSon()); //์Šฌ๊ธฐ์™€ ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด
    • ๊ฐ์ฒด ๋ฆฌํ„ฐ๋Ÿด์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด, home์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
  • ๋‘ ๋ฒˆ์งธ ์˜ˆ์ œ๋ฅผ this๋ฅผ ์‚ฌ์šฉํ•ด ์ˆ˜์ •ํ•ด ๋ณด์ž.

    function Home(mom) {
      //this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
      this.mom = mom;
    }
    
    Home.prototype.homeMember = function () {
      //this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
      return "๊ท€์—ฌ์šด ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด์™€ ์„ฑ์‹คํ•œ " + this.mom;
    };
    
    //์ธ์Šคํ„ด์Šค ์ƒ์„ฑ
    const home = new Home("์Šฌ๊ธฐ");
    
    console.log(home.homeMember()); // ๊ท€์—ฌ์šด ์ฝ”๋‚œ์ด์™€ ๋ชจ์นด์™€ ์„ฑ์‹คํ•œ ์Šฌ๊ธฐ
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ์ด์ฒ˜๋Ÿผ this๋Š” ์ƒํ™ฉ์— ๋”ฐ๋ผ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋Œ€์ƒ์ด ๋‹ค๋ฅด๋‹ค.
  • ์ž๋ฐ”๋‚˜ C++ ๊ฐ™์€ ํด๋ž˜์Šค ๊ธฐ๋ฐ˜ ์–ธ์–ด์—์„œ this๋Š” ์–ธ์ œ๋‚˜ ํด๋ž˜์Šค๊ฐ€ ์ƒ์„ฑํ•˜๋Š” ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค. ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ this๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๋Š” ๋ฐฉ์‹์— ๋”ฐ๋ผ this์— ๋ฐ”์ธ๋”ฉ ๋  ๊ฐ’, ์ฆ‰ this ๋ฐ”์ธ๋”ฉ์ด ๋™์ ์œผ๋กœ ๊ฒฐ์ •๋œ๋‹ค.

  • this๋Š” ์ฝ”๋“œ ์–ด๋””์—์„œ๋“  ์ฐธ์กฐ ๊ฐ€๋Šฅํ•˜๋‹ค. ์ „์—ญ์—์„œ๋„ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋„ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.

    //this๋Š” ์–ด๋””์„œ๋‚˜ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค.
    //์ „์—ญ์—์„œ this๋Š” window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    console.log(this); //window
    
    function square(num) {
      // ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ „์—ญ๊ฐ์ฒด์ธ window๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
      console.log(this); //window
      return num * num;
    }
    
    square(2);
    
    const member = {
      name: "ํฌ์žฌ",
      getName() {
        //๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
        console.log(this); // {name: "ํฌ์žฌ", getName: f}
        return this.name;
      },
    };
    console.log(member.getName()); //ํฌ์žฌ
    
    function member2(name) {
      this.name = "์ฐฌ๋ฏผ";
      //์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
      console.log(this); // {member2 {name: "์ฐฌ๋ฏผ"}}
    }
    
    const stuckyi = new member2("๋ณ‘๋ฏผ");
    • ํ•˜์ง€๋งŒ this๋Š” ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‚˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ธฐ ์œ„ํ•œ ์ž๊ธฐ ์ฐธ์กฐ ๋ณ€์ˆ˜์ด๋ฏ€๋กœ ์ผ๋ฐ˜์ ์œผ๋กœ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€ ๋˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ๋งŒ ์˜๋ฏธ๊ฐ€ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ strict mode๊ฐ€ ์ ์šฉ๋œ ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” undefined๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค. ์ผ๋ฐ˜ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.

      (function () {
        "use strict";
      
        function ex() {
          console.log(this); //undefined
        }
        ex();
      
        function Ex() {
          console.log(this); //Ex
        }
        new Ex();
      })();

2. ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹๊ณผ this ๋ฐ”์ธ๋”ฉ

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

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

  • ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ ๋ชจ๋“  ํ•จ์ˆ˜ (์ค‘์ฒฉ ํ•จ์ˆ˜, ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ํฌํ•จ) ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

    • ์ „์—ญ ํ•จ์ˆ˜

      function hello() {
        console.log("hello's this: ", this); //window
        function world() {
          console.log("world's this: ", this); //window
        }
        world();
      }
      hello();
    • ์ค‘์ฒฉ ํ•จ์ˆ˜

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

        //var ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ์ „์—ญ๋ณ€์ˆ˜ team์€ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๋‹ค.
        var team = "stuckyi";
        //const ํ‚ค์›Œ๋“œ๋กœ ์„ ์–ธํ•œ ์ „์—ญ๋ณ€์ˆ˜ team์€ ์ „์—ญ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ๊ฐ€ ์•„๋‹ˆ๋‹ค.
        //const team = "stuckyi";
        
        const cheerUp = {
          team: "flower",
          outer() {
            console.log("outer's this: ", this); //outer's this:  {team: 'flower', outer: ฦ’}
            console.log("outer's this.team: ", this.team); //outer's this.team:  flower
        
            //๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜
            function inner() {
              console.log("inner's this: ", this); //inner's this:  Window
              console.log("inner's this.team: ", this.team); //inner's this.team:  stuckyi
            }
        
            //๋ฉ”์„œ๋“œ ๋‚ด์—์„œ ์ •์˜ํ•œ ์ค‘์ฒฉ ํ•จ์ˆ˜๋„ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š”
            //์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
            inner();
          },
        };
        
        cheerUp.outer();
    • ์ฝœ๋ฐฑ ํ•จ์ˆ˜

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

        var team = "stuckyi";
        
        const cheerUp = {
          team: "flower",
          outer() {
            console.log("outer's this: ", this); //outer's this:  {team: 'flower', outer: ฦ’}
            //์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.
            setTimeout(function () {
              console.log("inner's this: ", this); //inner's this:  Window
              console.log("inner's this.team: ", this.team); //inner's this.team:  stuckyi
            }, 100);
          },
        };
        
        cheerUp.outer();
  • ๊ณ ๋กœ, ์–ด๋–ค ํ•จ์ˆ˜๋ผ๋„ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋˜๋ฉด this์— ์ „์—ญ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

  • ๊ทธ๋ ‡๋‹ค๋ฉด... ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ ์ค‘์ฒฉํ•จ์ˆ˜๋‚˜ ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this ๋ฐ”์ธ๋”ฉ์„ ๋ฉ”์„œ๋“œ์˜ this ๋ฐ”์ธ๋”ฉ๊ณผ ์ผ์น˜์‹œํ‚ค๋ ค๋ฉด?

    • ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜๊ธฐ

      var team = "stuckyi";
      
      const cheerUp = {
        team: "flower",
        outer() {
          // this ๋ฐ”์ธ๋”ฉ (cheerUp)์„ ๋ณ€์ˆ˜ that์— ํ• ๋‹นํ•œ๋‹ค.
          const that = this;
      
          //์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์—์„œ this๋Œ€์‹  that์„ ์ฐธ์กฐํ•œ๋‹ค.
          setTimeout(function () {
            console.log(that.team); //flower
          }, 100);
        },
      };
      
      cheerUp.outer();
    • Function.prototype.call, Function.prototype.bind ๋ฉ”์„œ๋“œ ์‚ฌ์šฉ

    • ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ ์‚ฌ์šฉ

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

2.2 ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

  • ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด, ์ฆ‰ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ด๋ฆ„ ์•ž์˜ ๋งˆ์นจํ‘œ(.) ์—ฐ์‚ฐ์ž ์•ž์— ๊ธฐ์ˆ ํ•œ ๊ฐ์ฒด๊ฐ€ ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค.

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

    const team = {
      name: "stuckyi",
      getName() {
        //๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.
        return this.name;
      },
    };
    
    //๋ฉ”์„œ๋“œ getName์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” team์ด๋‹ค.
    console.log(team.getName()); //stuckyi
    • ์œ„ ์˜ˆ์ œ์˜ getName ๋ฉ”์„œ๋“œ๋Š” team ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ์ •์˜๋˜์—ˆ๋‹ค. ๋ฉ”์„œ๋“œ๋Š” ํ”„๋กœํผํ‹ฐ์— ๋ฐ”์ธ๋”ฉ๋œ ํ•จ์ˆ˜๋‹ค. ์ฆ‰, team๊ฐ์ฒด์˜ getName ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด๋Š” taem ๊ฐ์ฒด์— ํฌํ•จ๋œ ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ๋…๋ฆฝ์ ์œผ๋กœ ์กด์žฌํ•˜๋Š” ๋ณ„๋„์˜ ๊ฐ์ฒด๋‹ค. getName ํ”„๋กœํผํ‹ฐ๊ฐ€ ํ•จ์ˆ˜ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์„ ๋ฟ์ด๋‹ค. ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ

    • ๋”ฐ๋ผ์„œ, getName ํ”„๋กœํผํ‹ฐ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ•จ์ˆ˜ ๊ฐ์ฒด, ์ฆ‰ getName ๋ฉ”์„œ๋“œ๋Š” ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ํ”„๋กœํผํ‹ฐ์— ํ• ๋‹นํ•˜๋Š” ๊ฒƒ์œผ๋กœ ๋‹ค๋ฅธ ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๊ณ , ์ผ๋ฐ˜ ๋ณ€์ˆ˜์— ํ• ๋‹นํ•˜์—ฌ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

      const anotherTeam = {
        name: "flower",
      };
      
      //getName ๋ฉ”์„œ๋“œ๋ฅผ anotherTeam ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋กœ ํ• ๋‹น
      anotherTeam.getName = team.getName;
      
      //getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” anotherTeam์ด๋‹ค.
      console.log(anotherTeam.getName()); //flower
      
      //getName ๋ฉ”์„œ๋“œ๋ฅผ ๋ณ€์ˆ˜์— ํ• ๋‹น
      const getName = team.getName;
      
      //getName ๋ฉ”์„œ๋“œ๋ฅผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ
      console.log(getName()); //''
      //์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ getName ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this.name์€ ๋ธŒ๋ผ์šฐ์ €ํ™˜๊ฒฝ์—์„œ window.name๊ณผ ๊ฐ™๋‹ค.
      //๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ ๊ธฐ๋ณธ๊ฐ’์€ '' ์ด๋‹ค.
      //Node.js ํ™˜๊ฒฝ์—์„œ this.name์€ undefined์ด๋‹ค.
      • ๋”ฐ๋ผ์„œ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” ํ”„๋กœํผํ‹ฐ๋กœ ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๊ฐ์ฒด์™€๋Š” ๊ด€๊ณ„๊ฐ€ ์—†๊ณ  ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ ๋œ๋‹ค. ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์™€ this
    • ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์—์„œ ์‚ฌ์šฉ๋œ this๋„ ์ผ๋ฐ˜ ๋ฉ”์„œ๋“œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํ•ด๋‹น ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด์— ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

      function Team(name) {
        this.name = name;
      }
      
      Team.prototype.getName = function () {
        return this.name;
      };
      
      const we = new Team("stuckyi");
      
      //getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” we๋‹ค.
      console.log(we.getName()); // โ‘  stuckyi
      
      Team.prototype.name = "flower";
      
      //getName ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” Person.prototype ์ด๋‹ค.
      console.log(Team.prototype.getName()); // โ‘ก flower
      • โ‘ ์˜ ๊ฒฝ์šฐ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” we๋‹ค. ๋”ฐ๋ผ์„œ, getName์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” we๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ this.name์€ 'stuckyi'์ด๋‹ค.
      • โ‘ก์˜ ๊ฒฝ์šฐ getName์„ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด๋Š” Team.prototype์ด๋‹ค. Team.prototype๋„ ๊ฐ์ฒด์ด๋ฏ€๋กœ ์ง์ ‘ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ, getName์˜ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ this๋Š” Team.prototype์„ ๊ฐ€๋ฆฌํ‚ค๋ฉฐ this.name์€ 'flower'์ด๋‹ค. ํ”„๋กœํ† ํƒ€์ž… ๋ฉ”์„œ๋“œ์™€ this

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

  • ์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์—๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ (๋ฏธ๋ž˜์—) ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๊ฐ€ ๋ฐ”์ธ๋”ฉ๋œ๋‹ค.

    function Team(member) {
      //์ƒ์„ฑ์ž ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
      this.member = member;
      this.getMember = function () {
        return "์Šคํˆฌํ‚ค ๋ฉค๋ฒ„์ธ " + this.member;
      };
    }
    
    //๋ฉค๋ฒ„๊ฐ€ ํฌ์žฌ์ธ stuckyi1 ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
    const stuckyi1 = new Team("ํฌ์žฌ");
    
    //๋ฉค๋ฒ„๊ฐ€ ์šฐ์˜์ธ stuckyi2 ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑ
    const stuckyi2 = new Team("์šฐ์˜");
    
    console.log(stuckyi1.getMember()); //์Šคํˆฌํ‚ค ๋ฉค๋ฒ„์ธ ํฌ์žฌ
    console.log(stuckyi2.getMember()); //์Šคํˆฌํ‚ค ๋ฉค๋ฒ„์ธ ์šฐ์˜
    • ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋Š” ์ด๋ฆ„ ๊ทธ๋Œ€๋กœ ๊ฐ์ฒด (์ธ์Šคํ„ด์Šค)๋ฅผ ์ƒ์„ฑํ•˜๋Š” ํ•จ์ˆ˜๋‹ค. ์ผ๋ฐ˜ ํ•จ์ˆ˜์™€ ๋™์ผํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•˜๊ณ  new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜๋ฉด ํ•ด๋‹น ํ•จ์ˆ˜๋Š” ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•œ๋‹ค.
    • ๋งŒ์•ฝ new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•œ๋‹ค.
    //new ์—ฐ์‚ฐ์ž์™€ ํ•จ๊ป˜ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฉด ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋กœ ๋™์ž‘ํ•˜์ง€ ์•Š๋Š”๋‹ค. ์ฆ‰, ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์˜ ํ˜ธ์ถœ์ด๋‹ค.
    const stuckyi3 = Team("์Šฌ๊ธฐ");
    
    //์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Team์—๋Š” ๋ฐ˜ํ™˜๋ฌธ์ด ์—†์œผ๋ฏ€๋กœ ์•”๋ฌต์ ์œผ๋กœ undefined๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
    console.log(stuckyi3); //undefined
    
    //์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ Team์˜ ๋‚ด๋ถ€์˜ this๋Š” ์ „์—ญ ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
    console.log(member); //์Šฌ๊ธฐ

2.1 Function.prototype.apply/call/bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ ํ˜ธ์ถœ

  • apply, call, bind๋Š” Function.prototype์˜ ๋ฉ”์„œ๋“œ๋‹ค. ์ฆ‰, ์ด๋“ค ๋ฉ”์„œ๋“œ๋Š” ๋ชจ๋“  ํ•จ์ˆ˜๊ฐ€ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

    2.1.1. Function.prototype.apply, Function.prototype.call

    • apply์™€ call์€ this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด์™€ ์ธ์ˆ˜ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ธ์ˆ˜๋กœ ์ „๋‹ฌ๋ฐ›์•„ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.

      function getThisBinding() {
        return this;
      }
      
      //this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
      const thisArg = { a: 1 };
      
      console.log(getThisBinding()); //window
      
      //getThisBinding ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ๊ฐ์ฒด๋ฅผ getThisBinding ํ•จ์ˆ˜์˜ this์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
      console.log(getThisBinding.apply(thisArg)); //{ a: 1 }
      console.log(getThisBinding.call(thisArg)); //{ a: 1 }
    • apply์™€ call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์— ์ธ์ˆ˜๋ฅผ ์ „๋‹ณํ•˜๋Š” ๋ฐฉ์‹๋งŒ ๋‹ค๋ฅผ ๋ฟ ๋™์ผํ•˜๊ฒŒ ๋™์ž‘ํ•œ๋‹ค.

      function getThisBinding() {
        console.log(arguments);
        return this;
      }
      
      //this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
      const thisArg = { a: 1 };
      
      //getThisBinding ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•  ๊ฐ์ฒด๋ฅผ getThisBinding ํ•จ์ˆ˜์˜ this์— ๋ฐ”์ธ๋”ฉํ•œ๋‹ค.
      
      //apply ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฌถ์–ด ์ „๋‹ฌํ•œ๋‹ค.
      console.log(getThisBinding.apply(thisArg, [1, 2, 3]));
      //[Arguments] { '0': 1, '1': 2, '2': 3 }
      //{ a: 1 }
      
      //call ๋ฉ”์„œ๋“œ๋Š” ํ˜ธ์ถœํ•  ํ•จ์ˆ˜์˜ ์ธ์ˆ˜๋ฅผ ์‰ผํ‘œ๋กœ ๊ตฌ๋ถ„ํ•œ ๋ฆฌ์ŠคํŠธ ํ˜•์‹์œผ๋กœ ์ „๋‹ฌํ•œ๋‹ค.
      console.log(getThisBinding.call(thisArg, 1, 2, 3));
      ////[Arguments] { '0': 1, '1': 2, '2': 3 }
      //{ a: 1 }
    • apply์™€ call ๋ฉ”์„œ๋“œ์˜ ๋Œ€ํ‘œ์ ์ธ ์šฉ๋„๋Š” arguments ๊ฐ์ฒด์™€ ๊ฐ™์€ ์œ ์‚ฌ ๋ฐฐ์—ด ๊ฐ์ฒด์— ๋ฐฐ์—ด ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒฝ์šฐ์ด๋‹ค. arguments ๊ฐ์ฒด๋Š” ๋ฐฐ์—ด์ด ์•„๋‹ˆ๊ธฐ ๋•Œ๋ฌธ์— Array.prototype.slice ๊ฐ™์€ ๋ฐฐ์—ด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์œผ๋‚˜, apply์™€ call ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ€๋Šฅํ•˜๋‹ค.

      function convertArgsToArray() {
      console.log(arguments);
      
            //arguments ๊ฐ์ฒด๋ฅผ ๋ฐฐ์—ด๋กœ ๋ณ€ํ™˜
            //Array.prototype.slice๋ฅผ ์ธ์ˆ˜ ์—†์ด ํ˜ธ์ถœํ•˜๋ฉด ๋ฐฐ์—ด์˜ ๋ณต์‚ฌ๋ณธ์„ ์ƒ์„ฑํ•œ๋‹ค.
            const arr = Array.prototype.slice.call(arguments);
            // const arr = Array.prototype.slice.apply(arguments);
            console.log(arr);
      
            return arr;
          }
      
          convertArgsToArray(1, 2, 3); //[ 1, 2, 3 ]
          ```
      
      <b>2.1.1. Function.prototype.bind</b>
    • bind ๋ฉ”์„œ๋“œ๋Š” apply์™€ call ๋ฉ”์„œ๋“œ์™€ ๋‹ฌ๋ฆฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๊ณ  this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋งŒ ์ „๋‹ฌํ•œ๋‹ค.

      function getThisBinding() {
        return this;
      }
      
      //this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด
      const thisArg = { a: 1 };
      
      //bind ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜์— this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.
      //bind ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š๋Š”๋‹ค.
      console.log(getThisBinding.bind(thisArg)); //getThisBinding
      //bind ๋ฉ”์„œ๋“œ๋Š” ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ช…์‹œ์ ์œผ๋กœ ํ˜ธ์ถœํ•ด์•ผ ํ•œ๋‹ค.
      console.log(getThisBinding.bind(thisArg)()); //{ a: 1 }
    • bind ๋ฉ”์„œ๋“œ๋Š” ๋ฉ”์„œ๋“œ์˜ this์™€ ๋ฉ”์„œ๋“œ ๋‚ด๋ถ€์˜ ์ค‘์ฒฉ ํ•จ์ˆ˜ ๋˜๋Š” ์ฝœ๋ฐฑ ํ•จ์ˆ˜์˜ this๊ฐ€ ๋ถˆ์ผ์น˜ํ•˜๋Š” ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค. ์˜ˆ๋กœ ๋“ค์–ด, ์•„๋ž˜์˜ ์˜ˆ์ œ๋ฅผ ๋ณด์ž.

      const Team = {
        name: "์ฐฌ๋ฏผ",
        babo(callback) {
          setTimeout(callback, 10000);
        },
      };
      
      Team.babo(function () {
        console.log(`์•ˆ๋…•~ ${this.name}`); //์•ˆ๋…•~ undefined
        //์ผ๋ฐ˜ ํ•จ์ˆ˜๋กœ ํ˜ธ์ถœ๋œ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this.name์€ ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ window.name๊ณผ ๊ฐ™๋‹ค.
        // ๋ธŒ๋ผ์šฐ์ € ํ™˜๊ฒฝ์—์„œ window.name์€ ๋ธŒ๋ผ์šฐ์ € ์ฐฝ์˜ ์ด๋ฆ„์„ ๋‚˜ํƒ€๋‚ด๋Š” ๋นŒํŠธ์ธ ํ”„๋กœํผํ‹ฐ์ด๋ฉฐ ๊ฐ’์€ ''์ด๋‹ค.
        //Node.js ํ™˜๊ฒฝ์—์„œ this.name์€ undefined ์ด๋‹ค.
      });
      • Team.babo์˜ ์ฝœ๋ฐฑํ•จ์ˆ˜๋Š” ์™ธ๋ถ€ ํ•จ์ˆ˜ Team.babo๋ฅผ ๋•๋Š” ํ—ฌํผ ํ•จ์ˆ˜(๋ณด์กฐ ํ•จ์ˆ˜) ์—ญํ• ์„ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์™ธ๋ถ€ ํ•จ์ˆ˜ Team.babo ๋‚ด๋ถ€์˜ this์™€ ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๊ฐ€ ์ƒ์ดํ•˜๋ฉด ๋ฌธ๋งฅ์ƒ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.

      • ๋”ฐ๋ผ์„œ, ์ฝœ๋ฐฑ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this๋ฅผ ์™ธ๋ถ€ ํ•จ์ˆ˜ ๋‚ด๋ถ€์˜ this์™€ ์ผ์น˜ ์‹œ์ผœ์•ผ ํ•œ๋‹ค. ์ด ๋•Œ, bind ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ this๋ฅผ ์ผ์น˜์‹œํ‚ฌ ์ˆ˜ ์žˆ๋‹ค.

        const Team = {
          name: "์ฐฌ๋ฏผ",
          babo(callback) {
            setTimeout(callback.bind(this), 10000);
          },
        };
        
        Team.babo(function () {
          console.log(`์•ˆ๋…•~ ${this.name}`); //์•ˆ๋…•~ ์ฐฌ๋ฏผ
        });

3. ์ •๋ฆฌ

ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹ this ๋ฐ”์ธ๋”ฉ
์ผ๋ฐ˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ „์—ญ ๊ฐ์ฒด
๋ฉ”์„œ๋“œ ํ˜ธ์ถœ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•œ ๊ฐ์ฒด
์ƒ์„ฑ์ž ํ•จ์ˆ˜ ํ˜ธ์ถœ ์ƒ์„ฑ์ž ํ•จ์ˆ˜๊ฐ€ (๋ฏธ๋ž˜์—) ์ƒ์„ฑํ•  ์ธ์Šคํ„ด์Šค
apply,call,bind ๋ฉ”์„œ๋“œ์— ์˜ํ•œ ๊ฐ„์ ‘ํ˜ธ์ถœ apply,call,bind ๋ฉ”์„œ๋“œ์— ์ฒซ๋ฒˆ์งธ ์ธ์ˆ˜๋กœ ์ „๋‹ฌํ•œ ๊ฐ์ฒด

์ถœ์ฒ˜ : ๋ชจ๋˜์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ deep dive