currying in JavaScript?
Evaluate functions with multiple arguments and decompose them into a sequence of functions with a single argument.
Using closureUsing Curringfunction add(a) {return function add1(b) {return function add2(c) {return a + b + c}}}const res = add(1);const newRes = res(2);const newResult = newRes(3);console.log(newResult) // it return 6
Currying is a function that takes one argument at a time and returns a new function expecting the next argument. It is a transformation of functions that translates a function from callable as f(a, b, c) into callable as f(a)(b)(c).
function add(a) { return function add1(b) { return function add2(c) { return a + b + c } } } const res = add(1)(2)(3); console.log(res) // it return 6
Using bind Method
a function that accepts multiple arguments. It will transform this function into a series of functions, where every little function will accept a single argument until all arguments are completed
Another example
userObj = {name : 'shreyash',age : 20,}function user(user){return function userInfo(e){return userObj[e]}}const result = user(userObj);console.log(result('name'))
shreyash
Post a Comment