๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Code-States

[D+44] call, apply, bind ๋ฉ”์†Œ๋“œ

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 10. 20.

 

D+44

-  call, apply, bind ๋ฉ”์†Œ๋“œ -

(call, apply, bind)

 


 

 

call ๊ณผ apply

call๊ณผ apply๋ฅผ ํ˜ธ์ถœํ•  ๊ฒฝ์šฐ ๋ช…์‹œ์ ์œผ๋กœ this๋ฅผ ์ง€์ •ํ•˜๊ณ  ์‹ถ์„๋•Œ ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ๊ฐ„๋žตํ•˜๊ฒŒ ์„ค๋ช…ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ์ฒซ๋ฒˆ์งธ ์ธ์ž๊ฐ€ ํ•ญ์ƒ this๊ฐ’์ด ๋ฉ๋‹ˆ๋‹ค.

 

์ด ๋‘˜์˜ ์ฐจ์ด์ ์€ .apply๋Š” ๋ฐฐ์—ด๋กœ ๊ฐ’์„ ๋„˜๊ฒจ์ฃผ๊ณ , .call์€ ๊ทธ๋ƒฅ ๋‹จ์ˆœํžˆ ๊ฐ’์„ ์ž…๋ ฅํ•˜๋ฉด ๋„˜๊ฒจ์ค€๋‹ค๋Š” ์ฐจ์ด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

function add(x, y) {
	return x + y;
}

add.apply(null, [2, 8]); // 10

add.call(null, 2, 8); // 10

 

 


bind

.bind๋Š” .call๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ this ๋ฐ ์ธ์ž๋ฅผ ๋ฐ”์ธ๋”ฉ ํ•˜์ง€๋งŒ ๋‹น์žฅ ์‹คํ–‰ํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹Œ ๋ฐ”์ธ๋”ฉ ๋œ ํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.

 

์ฒซ ๋ฒˆ์งธ ์ธ์ž๋Š” this, ๋‘๋ฒˆ์งธ ์ธ์ž๋ถ€ํ„ฐ๋Š” ํ•„์š”ํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ „๋‹ฌํ•œ๋‹ค.

 

fn.bind(this๊ฐ’, ์ธ์ž1, ์ธ์ž2, ...)

 

case 1: ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ

bind๋Š” ์ด๋ฒคํŠธ ํ•ธ๋“ค๋Ÿฌ์—์„œ ์ด๋ฒคํŠธ ๊ฐ์ฒด ๋Œ€์‹  ๋‹ค๋ฅธ ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ณ ์ž ํ•  ๋•Œ ์œ ์šฉํ•˜๊ฒŒ ์‚ฌ์šฉ๋œ๋‹ค.

<!DOCTYPE html>
<html>
  <body>
    <button id="btn"> ํด๋ฆญํ•˜์„ธ์š” </button>
  </body>  
</html>

html ํŒŒ์ผ

let btn = document.querySelector('#btn')
btn.onclick = handleClick

function handleClick() {
  console.log(this) // <button id="btn"> ํด๋ฆญํ•˜์„ธ์š” </button>
}

js ํŒŒ์ผ

 

bind๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ (gif๊ฐ€ ์ œ๋Œ€๋กœ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด gif๋ฅผ ํด๋ฆญํ•ด ํ™•์ธํ•ด ์ฃผ์„ธ์š”)

bind๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋ฐ”๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜๋ฉด ์ด๋ฒคํŠธ๊ฐ์ฒด๊ฐ€ ๋ฐ”๋กœ ๋‚˜์˜ค๊ฒ ์ง€๋งŒ

bind๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ๊ฒŒ ๋˜๋ฉด ๋‹ค๋ฅธ ๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

let btn = document.querySelector('#btn')

btn.onclick = handleClick.bind({ hello: 'world'})

function handleClick() {
  console.log(this) // {hello: 'world'}
}

js ํŒŒ์ผ

 

bind๋ฅผ ์‚ฌ์šฉํ•ด ๋‹ค๋ฅธ ๊ฐ’์„ ์ „๋‹ฌํ–ˆ์„ ๊ฒฝ์šฐ (gif๊ฐ€ ์ œ๋Œ€๋กœ ์‹คํ–‰๋˜์ง€ ์•Š๋Š”๋‹ค๋ฉด gif๋ฅผ ํด๋ฆญํ•ด ํ™•์ธํ•ด ์ฃผ์„ธ์š”)

 

case 2: setTimeout

setTimeout์€ ์‹œ๊ฐ„ ์ง€์—ฐ์„ ์ผ์œผํ‚จ ํ›„ ํ•จ์ˆ˜๋ฅผ ๋น„๋™๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ํ•˜๊ฒŒ ํ•˜๋Š” ํ•จ์ˆ˜๋กœ

๋ช…์‹œ์ ์œผ๋กœ ํ•ญ์ƒ window ๊ฐ์ฒด๋ฅผ this ๋ฐ”์ธ๋”ฉํ•˜๋Š” ํŠน์ง•์„ ๊ฐ€์ง€๊ณ  ์žˆ๋‹ค.

 

์•„๋ž˜์˜ ์ฝ”๋“œ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ setTimeout์œผ๋กœ ์ธํ•œ ํ•จ์ˆ˜๋กœ

ํ›„์— ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

class Rectangle {
  constructor(width, height) {
    this.width = width
    this.height = height
  }
  
  getArea() {
    return this.width * this.height
  }

  printArea() {
    console.log('์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋Š” ' + this.getArea() + ' ์ž…๋‹ˆ๋‹ค')
  }
  
  printSync() {
    // ์ฆ‰์‹œ ์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ์ฝ˜์†”์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค
    this.printArea()
  }
  
  printAsync() {
    // 1์ดˆ ํ›„ ์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋ฅผ ์ฝ˜์†”์— ํ‘œ์‹œํ•ฉ๋‹ˆ๋‹ค
    setTimeout(this.printArea, 2000)
  }
}

let box = new Rectangle(40, 20)
box.printSync() // '์‚ฌ๊ฐํ˜•์˜ ๋„“์ด๋Š” 800 ์ž…๋‹ˆ๋‹ค'
box.printAsync() // ์—๋Ÿฌ ๋ฐœ์ƒ!

bind๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š์€ ์‹คํ–‰๊ฒฐ๊ณผ์ฐฝ

 

 

์˜ค๋ฅ˜์—†์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์›ํ•œ๋‹ค๋ฉด ์…‹ํƒ€์ž„ ์•ˆ์— ์žˆ๋Š” this.printArea๋ฅผ

this.printArea.bind(this)๋กœ ์ˆ˜์ •ํ•ด ์ค€๋‹ค๋ฉด ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋  ๊ฒƒ์ด๋‹ค.

 

bind๋ฅผ ์‚ฌ์šฉํ•œ ์‹คํ–‰๊ฒฐ๊ณผ ์ฐฝ

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€