仅将第二个参数绑定到 javascript 函数

Bind only second argument to javascript function

var add = function(a, b) {

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

此处a的绑定值为1,b为4

如何在不使用展开运算符的情况下将绑定值 i.e)1 分配给函数的第二个参数(...)

您可以使用以下方式

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

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

此处 add5() 将为函数设置 x = 5

这将帮助您满足您的需求

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

你可以试试这个

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));

您可以使用绑定最终函数的交换函数。

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);

使用装饰器,如 georg 建议的那样。

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);

您可以使用 arguments 对象对参数进行重新排序。

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);