[JavaScript] call, apply, bind
๐ฉ call ๋ฉ์๋์ ํธ์ถ ์ฃผ์ฒด์ธ ํจ์๋ฅผ ์ฆ์ ์คํํ๋๋ก ํ๋ ๋ช
๋ น Function.prototype.call(thisArg[, arg1[, arg2[, ...]]]) ์ด๋ call ๋ฉ์๋์ ์ฒซ ๋ฒ์งธ ์ธ์๋ฅผ this๋ก ๋ฐ์ธ๋ฉํ๊ณ , ์ดํ์ ์ธ์๋ค์ ํธ์ถํ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ํ๋ค. ํจ์๋ฅผ ๊ทธ๋ฅ ์คํํ๋ฉด this๋ ์ ์ญ๊ฐ์ฒด๋ฅผ ์ฐธ์กฐํ์ง๋ง call ๋ฉ์๋๋ฅผ ์ด์ฉํ๋ฉด ์์์ ๊ฐ์ฒด๋ฅผ this๋ก ์ง์ ํ ์ ์๋ค. // call ๋ฉ์๋-1 var func = function (a, b, c) { console.log(this, a, b, c); }; func(1, 2, 3); // Window{ ... } 1 2 3 func.call({ x: 1 }, 4, 5, 6); // { x: 1 } 4 5 6 // call ๋ฉ์..
2021. 10. 23.