Javascript/Javascript

[์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ ์ค‘๊ธ‰] call, apply, bind

Rainbow๐ŸŒˆCoder 2022. 3. 28. 22:18
728x90

https://youtu.be/KfuyXQLFNW4

call, apply, bind

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ์˜ ํ•จ์ˆ˜ ํ˜ธ์ถœ ๋ฐฉ์‹

function add(a: number, b: number): number {
  return a + b;
}
console.log(add(10, 20));
console.log(add.apply(null, [10, 20]));
console.log(add.call(null, 10, 20));
console.log(add.bind(null, 10, 20)());

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

javascript์˜ ํ•จ์ˆ˜๋Š” ๊ฐ์ž ์ž์‹ ๋งŒ์˜ this๋ผ๋Š” ๊ฒƒ์„ ์ •์˜ํ•œ๋‹ค.

๊ธฐ๋ณธ์ ์œผ๋กœ this๋Š” window์ด์ง€๋งŒ this๋Š” ๊ฐ์ฒด ๋‚ด๋ถ€, ๊ฐ์ฒด ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ์‹œ, ์ƒ์„ฑ์ž new ํ˜ธ์ถœ์‹œ, ๋ช…์‹œ์  bind์‹œ์— ๋”ฐ๋ผ ๋ฐ”๋€๋‹ค.

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์—์„œ๋Š” ํ•จ์ˆ˜๋ฅผ ์–ด๋””์„œ ์–ด๋–ป๊ฒŒ ํ˜ธ์ถœํ–ˆ๋А๋ƒ์— ๊ด€๊ณ„์—†์ด this๊ฐ€ ๋ฌด์—‡์ธ์ง€ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋‹ค.

call, apply, bind ๋Š” ํ•จ์ˆ˜์˜ this๋ฅผ ๋ช…์‹œ์ ์œผ๋กœ ๋ฐ”์ธ๋”ฉ ํ• ๋•Œ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.

์œ ์šฉํ•˜๋ฉด์„œ ์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š” ํ•จ์ˆ˜์ด๊ณ , ํ˜•ํƒœ๋Š” ์กฐ๊ธˆ์”ฉ ๋‹ค๋ฅด๋‹ˆ ์ต์ˆ™ํ•ด ์ง€๋„๋ก ํ•˜์ž

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

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

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

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

showThisName.call(mike);
showThisName.call(tom);

updateBtith.call(mike, 1999, "singer");
console.log(mike);
updateBtith.apply(tom, [1995, "programmer"]);
console.log(tom);
const nums = [3, 100, 12, 342, 2];
const minNum = Math.min(...nums);
const maxNum = Math.max(...nums);
console.log(minNum); //2
console.log(maxNum); //342

apply ์‘์šฉํ•ด๋ณด๊ธฐ

const nums = [3, 100, 12, 342, 2];
//const minNum = Math.min(...nums);
const minNum = Math.min.apply(null, nums);
//const maxNum = Math.max(...nums);
const maxNum = Math.max.apply(null, nums);
console.log(minNum); //2
console.log(maxNum); //342

call ์‘์šฉํ•ด๋ณด๊ธฐ

const nums = [3, 100, 12, 342, 2];
//const minNum = Math.min(...nums);
//const minNum = Math.min.apply(null, nums);
const minNum = Math.min.call(null, ...nums);
//const maxNum = Math.max(...nums);
//const maxNum = Math.max.apply(null, nums);
const maxNum = Math.max.call(null, ...nums);
console.log(minNum); //2
console.log(maxNum); //342

๊ฒฐ๊ตญ ์ค‘์š”ํ•œ ๊ฒƒ์€

apply์™€ call์˜ ๋™์ž‘ ๋ฐฉ์‹์€ ๊ฐ™์€๋ฐ ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ๋ฐ›๋Š” ๋ฐฉ๋ฒ•๋งŒ ๋‹ค๋ฅผ ๋ฟ์ด๋‹ค.

bind

bindํ•จ์ˆ˜๊ฐ€ call, apply์™€ ๋‹ค๋ฅธ ์ ์€ ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•˜์ง€ ์•Š๋Š”๋‹ค๋Š” ์ ์ด๋‹ค. ๋Œ€์‹  boundํ•จ์ˆ˜๋ฅผ ๋ฆฌํ„ดํ•œ๋‹ค.

const mimi = {
  name: "MiMi",
};

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

const updateMiMi = update.bind(mimi);
updateMiMi(1995, "artist");
console.log(mimi);
const user = {
  name: "Kara",
  lookName: function () {
    console.log(`hello, ${this.name}`);
  },
};

user.lookName(); //hello, Kara

let fn = user.lookName;
fn.call(user); //hello, Kara

fn.apply(user); //hello, Kara

let boundFn = fn.bind(user);
boundFn(); //hello, Kara

์ ์šฉ ์‚ฌ๋ก€

const say = function() {
  console.log(this); // ์—ฌ๊ธฐ์„œ this๋Š” ๋ญ˜๊นŒ?
  console.log("Hello, my name is " + this.name);
};

say();

sayํ•จ์ˆ˜์—์„œ Window๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜๊ณ  ์‹ถ์ง€ ์•Š๋‹ค. ์ฆ‰, this๋ฅผ ๊ทธ๋•Œ ๊ทธ๋•Œ ์•Œ๋งž์€ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์„œ this๊ฐ’์— ๋”ฐ๋ผ ์ธ์‚ฌ๋ง์ด ํ•  ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด์„œ this๋ฅผ bindingํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ช…์‹œ์ ์œผ๋กœ ์œ„์˜ this๋ฅผ Window๊ฐ€ ์•„๋‹Œ ๋‹ค๋ฅธ ๊ฐ์ฒด๋กœ ๋ฐ”๊ฟ”์ฃผ๋Š” ํ•จ์ˆ˜๊ฐ€ call, apply, bind์ด๋‹ค.

728x90