๐Ÿ“– [JavaScript] call, apply, bind

ํ˜ฑยท2022๋…„ 9์›” 9์ผ

JavaScript_Intermediate

๋ชฉ๋ก ๋ณด๊ธฐ
14/19

๐Ÿ”Ž call, apply, bind : ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹๊ณผ ๊ด€๊ณ„์—†์ด this๋ฅผ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Œ


๐Ÿ“Œ call

๋ชจ๋“  ํ•จ์ˆ˜์—์„œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, this๋ฅผ ํŠน์ •๊ฐ’์œผ๋กœ ์ง€์ •ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const mike = {
  name: "Mike",
};

const tom = {
   name: "Tom",
};

function showThisName(){
  console.log(this.name);
}

showThisName() // ์—ฌ๊ธฐ์„œ this๋Š” window์ด๋ฏ€๋กœ, ์•„๋ฌด๊ฒƒ๋„ ์•ˆ ๋œธ
showThisName.call(mike); //Mike
//์—ฌ๊ธฐ์„œ this๊ฐ€ mike๊ฐ€ ๋œ ๊ฒƒ
  1. ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด์„œ call์„ ์‚ฌ์šฉํ•˜๊ณ , this๋กœ ์‚ฌ์šฉํ•  ๊ฐ์ฒด๋ฅผ ๋„˜๊ธฐ๋ฉด,
    ํ•ด๋‹น ํ•จ์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„ ๊ฐ์ฒด์˜ method์ธ ๊ฒƒ์ฒ˜๋Ÿผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Œ
  2. call์˜ ์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” this๋กœ ์‚ฌ์šฉ๋œ ๊ฐ’์ด๊ณ ,
    ๋งค๊ฐœ ๋ณ€์ˆ˜๊ฐ€ ๋” ์žˆ์œผ๋ฉด ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์ „๋‹ฌ ๋จ.
const mike = {
  name: "Mike",
};

const tom = {
   name: "Tom",
};

function showThisName(){
  console.log(this.name);
}

function upDate(birthYear, occupation){
  this.birthYear = birthYear;
  this.occupation = occupation;
};

upDate.call(mike, 1992, 'programmer');
console.log(mike); 
//{ name: "Mike", birthYear: 1992, occupation: 'programmer'}
//์ฒซ๋ฒˆ์งธ ๋งค๊ฐœ๋ณ€์ˆ˜: this ๊ฐ’, ๋‘๋ฒˆ์งธ ๋งค๊ฐœ ๋ณ€์ˆ˜: ํ•จ์ˆ˜๊ฐ€ ์‚ฌ์šฉํ•  ๋งค๊ฐœ๋ณ€์ˆ˜๋“ค์„ ์ˆœ์„œ๋Œ€๋กœ ์ ์€ ๊ฒƒ.

๐Ÿ“Œ apply

ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ์™ธํ•˜๋ฉด, call๊ณผ ์™„์ „ํžˆ ๊ฐ™์Šต๋‹ˆ๋‹ค.
call์€ ์ผ๋ฐ˜์ ์ธ ํ•จ์ˆ˜์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ์ง์ ‘ ๋ฐ›์ง€๋งŒ,
apply๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋ฅผ ๋ฐฐ์—ด๋กœ ๋ฐ›์Šต๋‹ˆ๋‹ค.

const mike = {
  name: "Mike",
};

const tom = {
   name: "Tom",
};

function showThisName(){
  console.log(this.name);
}

function upDate(birthYear, occupation){
  this.birthYear = birthYear;
  this.occupation = occupation;
};

upDate.apply(mike, [1992, 'programmer']);
console.log(mike); 
//{ name: "Mike", birthYear: 1992, occupation: 'programmer'}

apply๋Š” ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•  ๋•Œ ์œ ์šฉํ•จ!
apply๋Š” ๋ฐฐ์—ด์„ ์ „๋‹ฌํ•˜๋ฉด ๊ทธ ์ธ์ˆ˜๋ฅผ ์ฐจ๋ก€๋Œ€๋กœ ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉ

const nums = [3,10,1,6,4];

const minNum = Math.min(...nums);
const minNum = Math.min.call(null, ...nums);

const minNum = Math.min.apply(null, nums); //1

๐Ÿ“Œ bind

ํ•จ์ˆ˜์˜ this๊ฐ’์„ ์˜๊ตฌํžˆ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

const mike = {
  name: "Mike",
};

function upDate(birthYear, occupation){
  this.birthYear = birthYear;
  this.occupation = occupation;
};
//ํ•ญ์ƒ this ๊ฐ’์ด mike๊ฐ€ ๋˜๊ฒŒ ํ•ด๋ณด์ž!

const updateMike = update.bind(mike); 
//์ด ํ•จ์ˆ˜๋Š” ํ•ญ์ƒ ํ•ญ์ƒ this๋ฅผ mike๋กœ ์‚ฌ์šฉํ•˜๊ฒŒ ํ•จ

๐Ÿ”— ์ฝ”๋“œ ์˜ˆ์‹œ

const user = {
  name: "Mike",
  showName: function() {
    console.log(`hello, ${this.name}`);
  },
};

user.showName();
let fn = user.showName;
fn(); //์ด๋•Œ ์•„๋ฌด๊ฒƒ๋„ ๋‚˜์˜ค์ง€ ์•Š์Œ, this๋ฅผ ์žƒ์–ด๋ฒ„๋ฆฐ ๊ฒƒ.

fn.call(user); //this๋ฅผ ์‚ฌ์šฉํ•  user๋ฅผ ๋„ฃ์–ด์คŒ, ์ž˜๋‚˜์˜ด!
fn.apply(user);

let boundFn =fn.bind(user);

boundFn(); //hello, Mike ์ž˜ ๋‚˜์˜ด! 





profile
new blog: https://hae0-02ni.tistory.com/

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