how to call apply and bind in javascript - Lee-hyuna/33-js-concepts-kr GitHub Wiki

How-to: call() , apply() and bind() in JavaScript

원문 How-to: call() , apply() and bind() in JavaScript

이 글에선 κ°„λ‹¨ν•œ 예제λ₯Ό 톡해 μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ call(), apply() 그리고 bind() λ©”μ„œλ“œμ˜ 차이점을 λ…Όν•  것이닀. ν•¨μˆ˜λŠ” μžλ°”μŠ€ν¬λ¦½νŠΈμ—μ„œ Object이기 λ•Œλ¬Έμ—, 이 3가지 λ©”μ†Œλ“œλŠ” ν•¨μˆ˜μ˜ ν˜ΈμΆœμ„ μ œμ–΄ν•˜λŠ”λ° μ‚¬μš©λœλ‹€. ECMAScript3μ—λŠ” call() 및 apply()κ°€ λ„μž…λ˜μ—ˆκ³ , ECMAScript5μ—μ„œ bind()κ°€ μΆ”κ°€λ˜μ—ˆλ‹€.

μ‚¬μš©

call()/apply()λ₯Ό μ‚¬μš©ν•˜μ—¬ ν•¨μˆ˜λ₯Ό μ¦‰μ‹œ ν˜ΈμΆœν•  수 μžˆλ‹€. bind()λŠ” λ‚˜μ€‘μ— 싀행될 λ•Œ μ›λž˜μ˜ ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•˜λŠ” μ˜¬λ°”λ₯Έ μ»¨ν…μŠ€νŠΈ("this")λ₯Ό κ°–λŠ” 바인딩 ν•¨μˆ˜λ₯Ό λ°˜ν™˜ν•œλ‹€. κ·Έλž˜μ„œ bind()λŠ” ν•¨μˆ˜κ°€ νŠΉμ • μ΄λ²€νŠΈμ—μ„œ λ‚˜μ€‘μ— ν˜ΈμΆœλ˜μ–΄μ•Ό ν•  κ²½μš°μ— μœ μš©ν•˜κ²Œ μ‚¬μš©ν•  수 μžˆλ‹€.

μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ "this"λ₯Ό μ΄ν•΄ν•˜λ €λ©΄ Understanding "This" in JavaScript 글을 참쑰해라.

call() or Function.prototype.call()

call()에 λŒ€ν•œ κ°„λ‹¨ν•œ 예제λ₯Ό 확인해 보자.

//Demo with javascript .call()
var obj = {name:"Niladri"};

var greeting = function(a,b,c){
  return  "welcome "+this.name+" to "+a+" "+b+" in "+c;
};

console.log(greeting.call(obj,"Newtown","KOLKATA","WB"));
// returns output as welcome Niladri to Newtown KOLKATA in WB

call()λ©”μ„œλ“œμ˜ 첫번째 νŒŒλΌλ―Έν„°λŠ” ν•¨μˆ˜κ°€ ν˜ΈμΆœλ˜λŠ” λŒ€μƒμΈ "this" 값을 μ„€μ •ν•œλ‹€. μœ„μ˜ κ²½μš°μ—λŠ” "obj"이닀.

λ‚˜λ¨Έμ§€ λ§€κ°œλ³€μˆ˜λŠ” μ‹€μ œ ν•¨μˆ˜μ— λŒ€ν•œ λŒ€ν•œ μΈμˆ˜λ“€μ΄λ‹€.

apply() or Function.prototype.apply()

apply()에 λŒ€ν•œ κ°„λ‹¨ν•œ 예제λ₯Ό 확인해 보자.

//Demo with javascript .apply()
var obj = {name:"Niladri"};

var greeting = function(a,b,c){
  return  "welcome "+this.name+" to "+a+" "+b+" in "+c;
};

// array of arguments to the actual function
var args = ["Newtown","KOLKATA","WB"];
console.log("Output using .apply() below ")
console.log(greeting.apply(obj, args));

/* The output will be Output using .apply() below welcome Niladri to Newtown KOLKATA in WB */

d call() λ©”μ„œλ“œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ apply() λ©”μ„œλ“œμ˜ 첫 번째 λ§€κ°œλ³€μˆ˜λŠ” ν•¨μˆ˜κ°€ ν˜ΈμΆœλ˜λŠ” λŒ€μƒμΈ "this" 값을 μ„€μ •ν•œλ‹€. μœ„μ˜ κ²½μš°μ—” "obj" 객체이닀. apply()와 call()의 μœ μΌν•œ μ°¨μ΄λŠ” 두 번째 νŒŒλΌλ―Έν„°μ΄λ‹€. apply()λŠ” λ°°μ—΄λ‘œ μ‹€μ œ ν•¨μˆ˜μ— λŒ€ν•œ 인수λ₯Ό λ°›μ•„ 듀인닀.

bind() or Function.prototype.bind()

bind()에 λŒ€ν•œ κ°„λ‹¨ν•œ 예제λ₯Ό 확인해 보자.

//Use .bind() javascript
var obj = {name:"Niladri"};

var greeting = function(a,b,c){
  return  "welcome "+this.name+" to "+a+" "+b+" in "+c;
};

//creates a bound function that has same body and parameters
var bound = greeting.bind(obj);

console.dir(bound); ///returns a function

console.log("Output using .bind() below ");

console.log(bound("Newtown","KOLKATA","WB"));
//call the bound function

/* the output will be Output using .bind() below welcome Niladri to Newtown KOLKATA in WB */

μœ„μ˜ μ½”λ“œ μƒ˜ν”Œμ—μ„œ bind()λŠ” λ‚˜μ€‘μ— 호좜 될 μ»¨ν…μŠ€νŠΈκ°€ μžˆλŠ” λ°”μš΄λ“œ ν•¨μˆ˜λ₯Ό λ°˜ν™˜ν•˜κ³  μžˆλ‹€. λ‹€μŒκ³Ό 같이 μ½˜μ†”μ—μ„œ λ°”μš΄λ“œ ν•¨μˆ˜λ₯Ό λ³Ό 수 μžˆλ‹€.

/resource/yongkwan/15/01.png

bind() λ©”μ„œλ“œμ˜ 첫 번째 λ§€κ°œλ³€μˆ˜λŠ” 바인딩 ν•¨μˆ˜κ°€ 호좜될 λ•Œ λŒ€μƒ ν•¨μˆ˜μ˜ "this" 값을 μ„€μ •ν•œλ‹€. 바인딩 ν•¨μˆ˜κ°€ "new" μ—°μ‚°μžλ₯Ό μ‚¬μš©ν•˜μ—¬ κ΅¬μ„±λœ 경우 첫 번째 νŒŒλΌλ―Έν„°μ˜ 값은 λ¬΄μ‹œλœλ‹€λŠ” 점에 μœ μ˜ν•˜λΌ. λ‚˜λ¨Έμ§€ 맀개 λ³€μˆ˜λŠ” λŒ€μƒ ν•¨μˆ˜λ₯Ό 호좜 ν•  λ•Œ λ°”μΈλ“œ 된 ν•¨μˆ˜μ— 제곡된 인수 μ•žμ— μΆ”κ°€λ˜λŠ” 인수둜 μ „λ‹¬λœλ‹€.

μ—¬κΈ°κΉŒμ§€μ΄λ‹€. μ½μ–΄μ£Όμ–΄μ„œ 감사 λ“œλ¦¬λ©° 이 κ²Œμ‹œλ¬Όμ΄ μžλ°”μŠ€ν¬λ¦½νŠΈμ˜ apply(), call () 및 bind () λ©”μ†Œλ“œμ™€ κ΄€λ ¨λœ 문제λ₯Ό κ²ͺκ³ μžˆλŠ” μ΄ˆλ³΄μžμ—κ²Œ λ„μ›€μ΄λ˜κΈ°λ₯Ό λ°”λž€λ‹€.