๐ŸŽพ ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ 1์ฐจ ์ •๋ฆฌ (๊ฐ์ฒด)

KHWยท2021๋…„ 1์›” 18์ผ
0

Javascript ์ง€์‹์Œ“๊ธฐ

๋ชฉ๋ก ๋ณด๊ธฐ
13/95

๋ฏธ๋ฆฌ ์•Œ์•„์•ผํ•  ๊ฒƒ (์ค‘์š”)

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

๐Ÿ“ this ๋‹ค๋ฃจ๊ธฐ (์ค‘์š”ํ•œ ๋‚ด์šฉ)

๊ฐ์ฒด์•ˆ์—์„œ ์ด 5๊ฐœ์˜ ํ˜•ํƒœ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

  1. val : 10
  2. a () {}
  3. b : function(){}2์™€ 3์€ ๊ฐ™์€์˜๋ฏธ
  4. c : ()=>{}
  5. innerObj : {}
  • 2,3์€ ๊ฐ™์€ ์˜๋ฏธ
  • 4๋ฒˆ์€ ์ œ์ผ ์•ˆ์ค‘์š”

๊ฐ์ฒด์˜ ํ˜•ํƒœ ์ฝ”๋“œ ์‚ดํŽด๋ณด๊ธฐ

const obj = {
  val : 10,
  a(){
    console.log('a',this)
  },
  b : function(){
    console.log('b',this)
  },
  c : ()=>{
    console.log('c',this)
  },
  innerObj : {
    //์ƒˆ๋กœ์šด ๊ฐ์ฒด์•ˆ์ด ์กด์žฌ
    innerVal : 20,
    innerA(){
      console.log('innerA',this)
    },
    innerB : function(){
      console.log('innerB',this)
    },
    innerC : ()=>{
      console.log('innerC',this)
    },
  }
}

console.log(obj.val)
obj.a()
obj.b()
obj.c()

console.log(obj.innerObj.innerVal)
obj.innerObj.innerA()
obj.innerObj.innerB()
obj.innerObj.innerC()

๊ฒฐ๊ณผ

10

a {
  val: 10,
  a: [Function: a],
  b: [Function: b],
  c: [Function: c],
  innerObj: {
    innerVal: 20,
    innerA: [Function: innerA],
    innerB: [Function: innerB],
    innerC: [Function: innerC]
  }
}

b {
  val: 10,
  a: [Function: a],
  b: [Function: b],
  c: [Function: c],
  innerObj: {
    innerVal: 20,
    innerA: [Function: innerA],
    innerB: [Function: innerB],
    innerC: [Function: innerC]
  }
}

c {}

20

innerA {
  innerVal: 20,
  innerA: [Function: innerA],
  innerB: [Function: innerB],
  innerC: [Function: innerC]
}

innerB {
  innerVal: 20,
  innerA: [Function: innerA],
  innerB: [Function: innerB],
  innerC: [Function: innerC]
}

innerC {}

c์™€ innerC๋Š” ๊ฐ€๋Šฅํ•˜๋ฉด ์ƒ๊ฐํ•˜์ง€๋ง์ž ๋จธ๋ฆฌ๋งŒ ์•„ํ”„๋‹ค.

this์˜ ํ˜•ํƒœ ์‚ดํ”ผ๊ธฐ (๋ฉ”์†Œ๋“œ,ํ™”์‚ดํ‘œํ•จ์ˆ˜,์ผ๋ฐ˜ํ•จ์ˆ˜ this)

const obj = {
  a : function(){
    console.log(this)
    const b = ()=>{
      console.log(this);
    }
    function c (){
      console.log(this);
    }
    b()
    c()
  }
}

obj.a()

๊ฒฐ๊ณผ

{a: ฦ’}
{a: ฦ’}
Window {window: Window, self: Window, document: document, name: '', location: Location, โ€ฆ}
  1. obj์— ์†ํ•˜๋Š” a๋ฉ”์†Œ๋“œ๋Š” this๋ฅผ ๊ฐ์ฒด์ธ obj๋ฅผ ์ถœ๋ ฅ
  2. a๋ฉ”์†Œ๋“œ ์•ˆ์— ์†ํ•˜๋Š” ํ™”์‚ดํ‘œํ•จ์ˆ˜ b๋Š” ํ•ด๋‹น ํ™”์‚ดํ‘œํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š” ๋Œ€์ƒ(a๋ฉ”์†Œ๋“œ)๊ฐ€ ์œ„์น˜ํ•œ ๊ณณ์ด ๊ฐ์ฒด๋ผ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ์ง€์นญํ•œ๋‹ค.
  3. a๋ฉ”์†Œ๋“œ ์•ˆ์— ์†ํ•˜๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜๋Š” ์ผ๋ฐ˜ํ•จ์ˆ˜์ด๋ฏ€๋กœ window ๊ฐ์ฒด๋ฅผ ์ถœ๋ ฅ

๋œ ์ค‘์š”ํ•œ ๋‚ด์šฉ (์œ„์—๊ฐ€ ์ˆ˜์ •๋‚ด์šฉ)

ํ˜ผ์ž ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋ž‘ this๋ž‘ ํ•˜๋ฉด์„œ ๋ฉ˜๋ถ•์˜ค๋ฉด์„œ ํ•ด๋ณธ ์‹œ๋„์™€ ๊ฒฐ๊ณผ๋ฅผ ๋‚˜์—ดํ•ด๋ณธ๋‹ค.

ํ˜ผ์ž ํ•ด๋ณธ ๊ฒฐ๊ณผ ๊ฐ์ฒด ์•ˆ์—์„œ ๋ฉ”์†Œ๋“œ๊ฐ€ 4๊ฐ€์ง€์˜ ์–‘์ƒ์„ ๋„๋Š” ๊ฒฐ๊ณผ๊ฐ€ ์žˆ๋‹ค.

  1. ๊ฐ์ฒด์•ˆ์˜ ๋ฉ”์†Œ๋“œ์™€ ๊ทธ ๋ฉ”์†Œ๋“œ ์•ˆ์˜ ๋‚ด์žฅ ํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•  ๋•Œ
let case1 = {
	value:10,
	f : function(){			
		console.log(this);
		function inner(){
			console.log(this);
		}
		inner();
	}
  /*
  	f(){			
		console.log(this);
		function inner(){
			console.log(this);
		}
		inner();
	}
    */
}

1๋ฒˆ ๊ฒฐ๊ณผ case1.f()์‹คํ–‰
case1 ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ์ธ f๋Š” ์ถœ๋ ฅ๋Œ€์ƒ์ด this์ธ ์ž๊ธฐ์ž์‹ ์ธ case1 ๊ฐ์ฒด์ด๊ณ 
inner์˜ ์ผ๋ฐ˜ ๋‚ด์žฅํ•จ์ˆ˜์—์„œ์˜ this๋Š” ์ „์—ญ๊ฐ์ฒด์ด๋ฏ€๋กœ window ๊ฐ์ฒด์ด๋‹ค

{value: 10, f: ฦ’}
Window {window: Window, self: Window, document: document, name: "misaka", location: Location, โ€ฆ}

  1. ๊ฐ์ฒด์•ˆ์˜ ๋ฉ”์†Œ๋“œ์™€ ๊ทธ ๋ฉ”์†Œ๋“œ ์•ˆ์˜ ํ™”์‚ดํ‘œํ•จ์ˆ˜๊ฐ€ ์กด์žฌํ•  ๋•Œ
let case2 = {
	value:1000,
	f : function(){
		console.log(this);
		let inner = ()=>{console.log(this)};
		inner();
	}
  /*
  	f(){
		console.log(this);
		let inner = ()=>{console.log(this)};
		inner();
	}
    */
}

2๋ฒˆ ๊ฒฐ๊ณผ case1.f()์‹คํ–‰
1๋ฒˆ ๊ฒฐ๊ณผ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ฒซ๋ฒˆ์งธ this๋Š” case2 ๊ฐ์ฒด์ด๋‹ค.
inner๋Š” ํ™”์‚ดํ‘œํ•จ์ˆ˜์ด๋ฏ€๋กœ ์ด๋•Œ์˜ this๋Š” inner ํ•จ์ˆ˜๊ฐ€ ํ•ด๋‹น ์œ„์น˜์—์„œ ๊ฐ–๋Š” this์ด๋ฏ€๋กœ ์ฒซ๋ฒˆ์งธ this์™€ ๊ฐ™์€ case2 ๊ฐ์ฒด์ด๋‹ค.

{value: 1000, f: ฦ’}f: ฦ’ ()value: 1000__proto__: Object
{value: 1000, f: ฦ’}f: ฦ’ ()value: 1000__proto__: Object

  1. ๊ฐ์ฒด์•ˆ์˜ ๋Œ€์ƒ์ด (){}๊ฐ€ ์•„๋‹Œ {}๋กœ ๋˜์–ด์žˆ์„๋•Œ
let case1 = {
  value:10,
  f :{
    inner(){
      console.log(this);
    }
}
}

case1.f.inner() ์‹คํ–‰์‹œ this์ธ {inner: ฦ’}๊ฐ€ ์ถœ๋ ฅ


4. ๊ฐ์ฒด์•ˆ์˜ ํ™”์‚ดํ‘œํ•จ์ˆ˜๋กœ ๋ฉ”์†Œ๋“œ๊ฐ€ ์„ค์ • ๋˜์—ˆ์„ ๋•Œ (๊ฐ€๋Šฅํ•˜๋ฉด ์ง€์–‘)
let case3 = {
	f : ()=>{
		console.log(this);
	}
}

3๋ฒˆ ๊ฒฐ๊ณผ case1.f()์‹คํ–‰
case3์˜ ์ผ๋ฐ˜์ ์ธ ๋ฉ”์†Œ๋“œ๋กœ ์—ฐ๊ฒฐ ํ•  ์ˆ˜ ์—†๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ƒฅ window ๊ฐ์ฒด์ด๋‹ค.

Window {window: Window, self: Window, document: document, name: "misaka", location: Location, โ€ฆ}

๐Ÿ“ ๋‹ค๋ฅธ this ๋‹ค๋ฃจ๊ธฐ

๊ธฐ๋ณธ์ ์ธ ๋‚ด์šฉ

์˜๋„ํ•œ ๋‚ด์šฉ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

const obj = {
  
  firstMethod(){
    function secondFunc(){
      console.log(this)
    }
    const secondArrowFunc = ()=>{
      console.log(this)
    }
    secondFunc()
    secondArrowFunc()
  },
  
  firstObj : {
    
    secondMethod(){
      function thirdFunc(){
        console.log(this)
      }
      const thirdArrowFunc = () => {
        console.log(this)
      }
      thirdFunc()
      thirdArrowFunc()
    },
    
    secondObj : {
      
        thirdMethod(){
          function fourFunc(){
            console.log(this)
          }
          const fourArrowFunc = () => {
            console.log(this)
          }
          fourFunc()
          fourArrowFunc()
        }
    }
  }
}

obj.firstMethod()
obj.firstObj.secondMethod()
obj.firstObj.secondObj.thirdMethod()

ํ•ด๋‹น๊ฒฐ๊ณผ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.

๊ฐ๊ฐ์˜ ์ผ๋ฐ˜ํ•จ์ˆ˜์•ˆ์—์„œ์˜ ์ผ๋ฐ˜ ๋‚ด์žฅํ•จ์ˆ˜์˜ this๋Š” window๊ฐ์ฒด์ด๋ฏ€๋กœ 3๊ฐœ๋Š” window๊ฐ์ฒด๊ฐ€ ๋‚˜์™”๊ณ 

2๋ฒˆ์งธ ๊ฒฐ๊ณผ๋Š” secondArrowFunc()์˜ ๊ฒฐ๊ณผ๋กœ ํ•ด๋‹น this์˜ ์ƒ์œ„ ์œ„์น˜์ธ
firstMethod() ์œ„์น˜์˜ this์ด๋ฏ€๋กœ ๊ฐ์ฒด์ธ obj์ผํ…Œ๊ณ  ํ•ด๋‹น obj๋Š” firstObj์™€ firstMethod๋ฅผ ๊ฐ€์ ธ ์ด์™€๊ฐ™๋‹ค.

4๋ฒˆ์งธ ๊ฒฐ๊ณผ๋Š” thirdArrowFunc()์˜ ๊ฒฐ๊ณผ๋กœ ํ•ด๋‹น this์˜ ์ƒ์œ„ ์œ„์น˜์ธ
secondMethod() ์œ„์น˜์˜ this์ด๋ฏ€๋กœ ๊ฐ์ฒด์ธ firstObj์ผํ…Œ๊ณ  ํ•ด๋‹น firstObj๋Š” secondObj์™€ secondMethod๋ฅผ ๊ฐ€์ ธ ์ด์™€๊ฐ™๋‹ค.

6๋ฒˆ์งธ ๊ฒฐ๊ณผ๋Š” thirdArrowFunc()์˜ ๊ฒฐ๊ณผ๋กœ ํ•ด๋‹น this์˜ ์ƒ์œ„ ์œ„์น˜์ธ
secondMethod() ์œ„์น˜์˜ this์ด๋ฏ€๋กœ ๊ฐ์ฒด์ธ secondObj์ผํ…Œ๊ณ  ํ•ด๋‹น secondObj๋Š” thirdObj๋ฅผ ๊ฐ€์ ธ ์ด์™€๊ฐ™๋‹ค.


์ •๋ฆฌ

  • Aํ•จ์ˆ˜๋‚ด์˜ ์ผ๋ฐ˜ํ•จ์ˆ˜B๋Š” window๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
  • Aํ•จ์ˆ˜๋‚ด์˜ ํ™”์‚ดํ‘œํ•จ์ˆ˜B๋Š” Aํ•จ์ˆ˜๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒํƒœ์˜ ์œ„์น˜๋ฅผ this๋กœ ๊ฐ€๋ฅดํ‚จ๋‹ค.
    ->์ด๋•Œ์˜ ์œ„์น˜๊ฐ€ ๊ฐ์ฒด๋‚ด๋ถ€๋ผ๋ฉด ํ•ด๋‹น ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
    ->์ด๋•Œ์˜ ์œ„์น˜๊ฐ€ ๋”ฐ๋กœ ํฌํ•จ์ด ์—†๋‹ค๋ฉด ์ „์—ญ๊ฐ์ฒด๋ฅผ ๊ฐ€๋ฅดํ‚จ๋‹ค.
profile
๋‚˜์˜ ํ•˜๋ฃจ๋ฅผ ๊ฐ€๋Šฅํ•œ ๊ธฐ์–ตํ•˜๊ณ  ์ฆ๊ธฐ๊ณ  ํ›„ํšŒํ•˜์ง€๋ง์ž

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