๐ฉcall( )์ ์ด๋ฏธ ํ ๋น๋์ด์๋ ๋ค๋ฅธ ๊ฐ์ฒด์ ํจ์ ๋๋ ๋ฉ์๋๋ฅผ ํธ์ถํ๋ ํด๋น ๊ฐ์ฒด์ ์ฌํ ๋นํ ๋ ์ฌ์ฉ๋๋ค.
โthisโ๋ ํธ์ถํ๊ณ ์๋ ๊ฐ์ฒด ๋ฅผ ์ฐธ์กฐํ๋ค.
๋ฉ์๋๋ฅผ ํ๋ฒ ์์ฑํ๋ฉด, ์ ๊ฐ์ฒด๋ฅผ ์ํ ๋ฉ์๋๋ฅผ ์ฌ์์ฑํ ํ์ ์์ด call์ ์ด์ฉํด ๋ค๋ฅธ ๊ฐ์ฒด์ ์์ํ ์ ์๋ค.
โ
const kim = {
name: 'kim',
first: 10,
second: 20,
}
const lee = {
name: 'lee',
first: 10,
second: 10,
}
function sum(){
return this.first + this.second;
}
console.log(sum.call(kim)); //30
console.log(sum.call(lee)); //20
โ
function add(pre){
return pre + (this.first + this.second);
}
console.log(sum.call(kim, '=> ')); //=> 30
console.log(sum.call(lee, ': ')); //: 20