5
var add = function(a, b) {

    return a + b;
}
var addOne =add.bind(null,1);
var result = addOne(4);
console.log(result);

Here the binded value of a is 1 and b is 4.

How to assign the binding value i.e)1 to the second argument of the function without using spread operator(...)

Akshara
  • 127
  • 1
  • 2
  • 12
  • can you post your entire code ?? – Selvakumar Aug 03 '17 at 13:12
  • You'd have to write your own version of `.bind()`. The only tool I've ever seen that could do this was the [functional.js](http://functionaljs.com/) library, but that API does not seem to be there anymore. It's an unusual thing to do. – Pointy Aug 03 '17 at 13:13
  • 1
    have you looked at this question https://stackoverflow.com/questions/27699493/javascript-partially-applied-function-how-to-bind-only-the-2nd-parameter – Ali Ashraf Aug 03 '17 at 13:13
  • "_binding value_ " is somewhat confusing here. `bind` sets the `this` value used in the function, the rest of the arguments passed to `bind` are ... well, arguments to the function to call. – Teemu Aug 03 '17 at 13:15

4 Answers4

4

You could take a swap function with binding the final function.

var add = function (a, b) { console.log(a, b); return a + b; },
    swap = function (a, b) { return this(b, a); },
    addOne = swap.bind(add, 1),
    result = addOne(4);

console.log(result);

With decorator, as georg suggested.

var add = function (a, b) { console.log(a, b); return a + b; },
    swap = function (f) { return function (b, a) { return f.call(this, a, b) }; },
    addOne = swap(add).bind(null, 1),
    result = addOne(4);

console.log(result);

You could use the arguments object for reordering the parameters.

var add = function (a, b, c, d, e) {
        console.log(a, b, c, d, e);
        return a + b + c + d + e;
    },
    swap = function (f) {
        return function () { 
            var arg = Array.apply(null, arguments);
            return f.apply(this, [arg.pop()].concat(arg));
        };
    },
    four = swap(add).bind(null, 2, 3, 4, 5),
    result = four(1);

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • 2
    Interesting approach, I'd make `swap` a decorator, so it could be used like `swap(add).bind(...)`. – georg Aug 03 '17 at 13:31
  • What if I have 'n' number of arguments and have to bind the values except the first argument – Akshara Aug 04 '17 at 11:39
0

You can use the following way

var add = function(x){
    return function(y){
        return x+y;
    }
}

add(2)(3); // gives 5
var add5 = add(5);
add5(10); // gives 15

here add5() would set x = 5 for the function

marvel308
  • 10,288
  • 1
  • 21
  • 32
0

This will help you what you need

var add = function(a) {
    return function(b) {
        return a + b;
    };
}
var addOne = add(1);
var result = addOne(4);
console.log(result);
Selvakumar
  • 527
  • 2
  • 13
0

You can try this

function add (n) {
    var func = function (x) {
        if(typeof x==="undefined"){
           x=0;
        }
        return add (n + x);
    };

    func.valueOf = func.toString = function () {
        return n;
    };

    return func;
}
console.log(+add(1)(2));
console.log(+add(1)(2)(3));
console.log(+add(1)(2)(5)(8));
Sourabh Somani
  • 2,138
  • 1
  • 13
  • 27