๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ“’ JavaScript

JavaScript ํ•จ์ˆ˜ํ˜ธ์ถœ Call / Bind ๋ฉ”์„œ๋“œ

by Tamii 2021. 3. 11.
๋ฐ˜์‘ํ˜•

์ฒ˜์Œ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ๋ฐฐ์šธ ๋• ์“ฐ๋ผ๋Š”๋Œ€๋กœ ํ•จ์ˆ˜๋ฅผ ์“ฐ๊ณ  this ๋„ ๊ทธ๋ƒฅ์ผ์ง€๋งŒ, 

๋ช‡๊ฐœ์˜ ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฒฝํ—˜ํ•˜๊ณ  ๋‚˜์„œ ๋‹ค์‹œ ๊ณต๋ถ€ํ•ด๋ณด๋Š” JS๋Š” ์ •๋ง ์‹ ๊ธฐํ•˜๊ณ  ์žฌ๋ฐŒ๋Š” ๋„๊ตฌ์ด๊ตฌ๋‚˜ .!

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ํ•จ์ˆ˜

JS์˜ ํ•จ์ˆ˜ : ์ž์‹ ๋งŒ์˜ this๋ฅผ ์ •์˜ํ•˜๋Š”๊ฒƒ

 

์˜ˆ์‹œ๋กœ sayHello ๋ผ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  this๋ฅผ ์‹คํ–‰์‹œํ‚ค๋ฉด Window ๊ฐ์ฒด๊ฐ€ ๋‚˜์˜จ๋‹ค.

์ฆ‰ JS์—์„œ ๊ธฐ๋ณธ์ ์ธ this = windowr๊ฐ์ฒด  ๋ผ๋Š”๊ฒƒ 

๋‚˜๋Š” sayHello์—์„œ window๋ฅผ ์“ฐ๊ณ ์‹ถ์ง€ ์•Š๋‹ค. this๋ฅผ ๊ทธ๋•Œ๊ทธ๋•Œ ์•Œ๋งž์€ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์•ผ ํ•˜๋Š”๋ฐ ์ด๊ฒƒ์ด ๋ฐ”๋กœ 

this์˜ binding!

this๋ฅผ  ์•Œ๋งž๊ฒŒ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ ์žˆ๋Š”๋ฐ ๊ทธ๊ฒƒ์ด ๋ฐ”๋กœ call apply, bind์ด๋‹ค .

 


 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŽ์ด ๋‹ค๋ฃจ๊ณ  this๋ฅผ ๋‹ค๋ค„์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์Œ!!

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ ํ•  ์ˆ˜ ์žˆ๋‹ค.

์ผ๋ฐ˜์ ์œผ๋กœ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ˜ธ์ถœํ˜•ํƒœ์— ๋”ฐ๋ผ this ๊ฐ€ ๋‹ฌ๋ผ์ง€์ง€๋งŒ,

call, apply, bind ๋ฉ”์„œ๋“œ์—์„œ๋Š” ํ•จ์ˆ˜์˜ this๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ˜ธ์ถœํ•œ๋‹ค.

 

 

์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ๋ฒ•

sum(์ด ๋ถ€๋ถ„) 

sum ํ•จ์ˆ˜ ํ˜ธ์ถœ ์‹œ (  ) ์•ˆ์— ์ธ์ž๋ฅผ ์ „๋‹ฌํ•ด ํ˜ธ์ถœํ•œ๋‹ค.

function sum(){
    return a+b
}


sum(2,3)

 

 

call

ํ•จ์ˆ˜๋ฅผ ๋ฐ”๋กœ ์‹คํ–‰

์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค ๊ฐ์ฒด์˜ this๊ฐ’์„ ๋ฐ”๊พธ๋Š” (context๋ฐ”๊พธ๋Š”) ๋ฉ”์„œ๋“œ

 

๋ชจ๋“  ํ•จ์ˆ˜๋Š” call์ด๋ผ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์Œ  (ํ•จ์ˆ˜๋„ ๊ฐ์ฒด์ด๊ธฐ ๋•Œ๋ฌธ์— )

 

A. call ( "this ๋กœ ์ง€์ •ํ•  ๊ฒƒ", "ํŒŒ๋ผ๋ฏธํ„ฐ์ธ์ž๊ฐ’")
sum.call( this ๋Œ€์ฒด ๊ฐ’ , ์ธ์ž)
sum.call('kim')

 

 

์˜ˆ์ œ 1)

var kim = {name: 'kim', first:10, second:20}
var lee = {name: 'lee', first:10, second:10}


function sum(){
    return this.first + this.second;
}


//sum ์ด๋ผ๋Š” ๊ฐ์ฒด๋ฅผ ์‹คํ–‰์‹œํ‚ค๋Š” 
// sum.call( )


sum.call(kim); //this=kim  30

sum์ด๋ผ๋Š” function์€ kim์ด๋ผ๋Š” ๊ฐ์ฒด์˜ ๋ฉค๋ฒ„๊ฐ€ ์•„๋‹ˆ์—ˆ์ง€๋งŒ 

ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ ์ฒซ๋ฒˆ์งธ ์ธ์ž๋กœ sum์ด ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•  this์˜ ๊ฐ’์„ kim์œผ๋กœ ์ง€์ •ํ•˜๋‹ˆ 

sum์ด๋ผ๋Š” ํ•จ์ˆ˜๋Š” Kim์˜ ๋ฉค๋ฒ„์ธ ๋ฉ”์„œ๋“œ๊ฐ€ ๋จ 

 

 

์˜ˆ์ œ 2)

function Product(name, price) {
  this.name = name;
  this.price = price;
}

function Food(name, price) {
  Product.call(this, name, price);
  this.category = 'food';
}

console.log(new Food('cheese', 5).name);
//"cheese"

 

Food ์—์„œ 

Product์˜ this ๋Š” Food ์˜ this ์•ผ! ๋ผ๊ณ  ์„ ์–ธ


 

bind

ํ•จ์ˆ˜์˜ ๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•  this๋ฅผ ์˜๊ตฌ์ ์œผ๋กœ ๊ณ ์ •ํ•  ์ƒˆ๋กœ์šด ํ•จ์ˆ˜ ์ƒ์„ฑ

 

A.bind("๋‚ด๋ถ€์ ์œผ๋กœ ์‚ฌ์šฉํ•  this", "์ธ์ž๊ฐ’")
sum. bind ( sum์˜ this ๋กœ ์˜๊ตฌํžˆ ์ง€์ •ํ•  ๊ฒƒ sum์˜ ํŒŒ๋ผ๋ฏธํ„ฐ ์ธ์ž๊ฐ’  )
sum.bind( kim )

 

var kim = {name: 'kim', first:10, second:20}
var lee = {name: 'lee', first:10, second:10}

function sum(prefix){
    return prefix +(this.first + this.second);
}



var kimSum = sum.bind(kim,"->");
console.log(kimSum()); // -> 30

sum์ด ๋ฐ”๋€๊ฒƒ X

์ƒˆ๋กœ์šด ํ•จ์ˆ˜ (kimSum) ๋กœ ๋ฐ”๋€Œ์–ด ์ƒ์„ฑ๋œ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— sum (์›๋ž˜ ํ•จ์ˆ˜)์— ์˜ํ–ฅ์„ ์ฃผ์ง€ ์•Š์Œ 

 

 

 

๐Ÿง ์ƒ๊ฐํ•ด๋ณผ ๊ฒƒ 

  call bind
๊ณตํ†ต์  this๋ฅผ ์ง€์ •ํ•˜์—ฌ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœ
์ฐจ์ด์  ํ•จ์ˆ˜ ํ˜ธ์ถœ ํ•จ์ˆ˜ ์ƒ์„ฑ
  ์‹คํ–‰ํ•  ๋•Œ๋งˆ๋‹ค
๊ฐ์ฒด์˜ this ๊ฐ’์„ ๋ฐ”๊พธ๋Š” ๋ฉ”์„œ๋“œ
์˜๊ตฌ์ ์œผ๋กœ  this ๊ฐ’์„ ๊ณ ์ •์‹œํ‚จ
์ƒˆ๋กœ์šด  ํ•จ์ˆ˜๋ฅผ ์ƒ์„ฑํ•˜๋Š” ๋ฉ”์„œ๋“œ

 

 

javascript์—์„œ ํ•จ์ˆ˜๋ž€ ์–ด๋–ค ์กด์žฌ์ธ๊ฐ€ 

ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ new๋ฅผ ๋ถ™์ด๋ฉด ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์ƒ์„ฑ์ž๊ฐ€ ๋จ

ํŠน์ •ํ•œ ๊ฐ์ฒด๋‚˜ class ์— ์ข…์†๋˜์ง€ ์•Š๊ณ  ํ•„์š”์— ๋”ฐ๋ผ ์–ด๋–ค ๊ฐ์ฒด์˜ ๋ฉ”์„œ๋“œ๋„ ๋  ์ˆ˜๋„ ์žˆ๋Š” ๊ฒƒ 

 


์ƒ์† extends 

ES5 : Object.create

ES6 : extends

 

 

 

๊ฐ์ฒด์—์„œ call ๋กœ ์ƒ์†(extend) ๋งŒ๋“ค์–ด ๋ณด๊ธฐ 

 

function Car(brand) {		
this.carname = brand;		
}		
		
Car.prototype.present = function() {		
return 'I have a ' + this.carname;		
}		
		
//Model์ƒ์„ฑ์ž์™€ prototype์— show ๋ฉ”์„œ๋“œ ์ถ”๊ฐ€.
function Model(brand, mod) {
    Car.call(this,brand);		
    this.model = mod;		
}		

//extends ์—ญํ•  
Model.prototype = Object.create(Car.prototype);
	
	
Model.prototype.show = function() {		
//debugger;
return this.present() + ', it is a ' + this.model;		
}		
		
	
		
//ํ˜ธ์ถœ!!
const mycar = new Model("Ford", "Mustang");		
mycar.show();	// "I have a Ford, it is a Mustang"

 

function Model(brand, mod) {
    Car.call(this,brand);		
    this.model = mod;		
}		
	

this

Car์˜ ๊ฐ์ฒด๋ฅผ  ๋‚ด ๊ฒƒ(Model )๊ฑธ๋กœ ๊ฐ€์ ธ์˜ค๋Š” ๊ฒƒ 

Model.prototype = Object.create(Car.prototype);

 

 

const mycar = new Model("Ford", "Mustang");	

Model ์€ function์ด์ง€๋งŒ new Model ๋กœ ๋ถ€๋ฅด๋ฉด ์ƒ์„ฑ์ž๊ฐ€ ๋จ 

๋”ฐ๋ผ์„œ Model ์€ ์ƒ์„ฑ์ž 

 

๊ทธ๋Ÿฐ๊ฒŒ mycar์„ ๋ณด๋ฉด Car ์˜ constructor์ž„ 

Object.create๋ฅผ ํ–‡๊ธฐ ๋•Œ๋ฌธ

 

 

์ฐธ๊ณ ํ•œ ์ž๋ฃŒ 

medium.com/@lyslg1018/javascript-call-apply-bind-f6768af01a52

 

[javascript]call, apply, bind

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ ํ•จ์ˆ˜๋ฅผ ๋‹ค๋ฃจ๋‹ค ๋ณด๋ฉด this ๋ฅผ ๋‹ค๋ค„์•ผ ํ•  ๊ฒฝ์šฐ๊ฐ€ ๋งŽ์ด ์ƒ๊ธฐ๊ฒŒ ๋œ๋‹ค. ์ด๋Ÿฌํ•œ this๋Š” ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ, ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœํ–ˆ๋Š๋ƒ์™€ ๊ด€๊ณ„์—†์ด ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ์ง€์ •์„ ํ•ด์ค„์ˆ˜ ์žˆ๋‹ค

medium.com

www.youtube.com/watch?v=wOiteGyan-I

๋Œ“๊ธ€