Using Function.prototype.bind with an array of arguments?

后端 未结 10 1853
春和景丽
春和景丽 2020-12-08 01:57

How can I call Function.prototype.bind with an array of arguments, as opposed to hardcoded arguments? (Not using ECMA6, so no spread operator).

I\'m trying to put a

10条回答
  •  一生所求
    2020-12-08 02:40

    .bind is a normal function, so you can call .apply on it.
    All you have to do is pass the original function as the first param and the desired THIS variable as the first item in the array of arguments:

    bound = db.find.bind.apply(db.find, [null].concat(arguments));
    //      ^-----^            ^-----^   THIS
    

    Whether that can be considered cleaner or not is left to the reader.

提交回复
热议问题