2019/09/23 TIL(Today I Learn) - Currying ํจ์์์ arguments, rest parameter ์ฌ์ฉํ๊ธฐ
- September 23, 2019
- 1 minute read
Rest Parameter
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/rest_parameters
function currying (func) {
return function (...args) { // ๋ ์คํธ ํ๋ผ๋ฏธํฐ๋ ํจ์์ ํ๋ผ๋ฏธํฐ๋ก ์ถ๊ฐ๋์ด์ผ ํฉ๋๋ค.
return func(...args); // "args"๋ ๋ฐฐ์ด์ด ๋๊ธฐ ๋๋ฌธ์ "โฆargs"๋ก ์์ฑํ์ฌ ๋ฐฐ์ด์ ํด์ฒดํ ๋ค ์ธ์๋ก ์ฌ์ฉํฉ๋๋ค.
}
}
let plus = currying (function(x, y, z){ return x + y + z });
plus(1,2,3) // 6
arguments
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Functions/arguments
function currying (func) {
return function () {
return func(...arguments); // "arguments"๋ ์ ์ฌ ๋ฐฐ์ด์ด๊ธฐ ๋๋ฌธ์ "โฆarguments"๋ก ์์ฑํ์ฌ ๋ฐฐ์ด์ ํด์ฒดํ ๋ค ์ธ์๋ก ์ฌ์ฉํฉ๋๋ค.
}
}
let plus = currying (function(x, y, z){ return x + y + z });
plus(1,2,3) // 6
Function.prototype.apply()
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/apply
function currying (func) {
return function () {
return func.apply(null, arguments); // apply ๋ฉ์๋๋ ์ธ์๋ฅผ ๋ฐฐ์ด๋ก ๋ฐ๊ธฐ ๋๋ฌธ์ "arguments"๋ก ์์ฑํ์ฌ ์ฌ์ฉํฉ๋๋ค.
}
}
let plus = currying (function(x, y, z){ return x + y + z });
plus(1,2,3) // 6
Function.prototype.call()
https://developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Function/call
function currying (func) {
return function () {
return func.call(null, ...arguments); // call ๋ฉ์๋๋ ์ธ์๋ฅผ ์ผํ(,)๋ก ๊ตฌ๋ถํ์ฌ ๋ฐ๊ธฐ ๋๋ฌธ์ โ...arguments"๋ก ์์ฑํ์ฌ ๋ฐฐ์ด์ ํด์ฒดํ ๋ค ์ฌ์ฉํฉ๋๋ค.
}
}
let plus = currying (function(x, y, z){ return x + y + z });
plus(1,2,3) // 6