Using Function.prototype.bind with an array of arg

2019-01-16 23:20发布

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 promises wrapper around a module that uses callbacks and I want to bind all of the arguments passed in to my wrapper method and bind them. Then I want to call the partially applied bound function with my own callback, which will resolve or reject a promise.

var find = function() {
  var deferred, bound;
  deferred = Q.defer();
  bound = db.find.bind(null, arguments);
  bound(function(err, docs) {
    if(err) {
      deferred.fail(err);
    } else {
      deferred.resolve(docs);
    }
  });
  return deferred.promise;
}

But obviously this doesn't work because bind expects arguments rather than an array of arguments. I know I could do this by inserting my callback onto the end of the arguments array and using apply:

arguments[arguments.length] = function(err, docs) { ... }
db.find.apply(null, arguments);

Or by iterating over the arguments array and rebinding the function for each argument:

var bound, context;
for(var i = 0; i < arguments.length; i++) {
   context = bound ? bound : db.find;
   bound = context.bind(null, arguments[i]);
}
bound(function(err, docs) { ... })

But both of these methods feel dirty. Any ideas?

10条回答
神经病院院长
2楼-- · 2019-01-16 23:30

If someone is looking for an abstract sample:

var binded = hello.apply.bind(hello,null,['hello','world']);

binded();

function hello(a,b){
  console.log(this); //null
  console.log(a); //hello
  console.log(b); //world
}
查看更多
forever°为你锁心
3楼-- · 2019-01-16 23:36

Why not simply bind to the arguments array as per your example, and have the bound() function treat it just like that, as an array?

By the looks of your usage, you are then passing in a function as the final argument to bound(), which means by passing in the actual argument array, you avoid having to separate arguments from callbacks inside bound(), potentially making it easier to play with.

查看更多
对你真心纯属浪费
4楼-- · 2019-01-16 23:38

For those using ES6, Babel compiles:

db.find.bind(this, ...arguments)

to:

db.find.bind.apply(db.find, [this].concat(Array.prototype.slice.call(arguments)));

I think it's fair to say Babel is pretty definitive. Credit to @lorenz-lo-sauer though, it's almost identical.

查看更多
We Are One
5楼-- · 2019-01-16 23:38

A definitive and simple answer might be

Function.apply.bind(this.method, this, arguments);

Kinda "hard" to grasp, yet, neat.

查看更多
成全新的幸福
6楼-- · 2019-01-16 23:39

The following is a common snippet of code I use in all my projects:

var bind = Function.bind;
var call = Function.call;

var bindable = bind.bind(bind);
var callable = bindable(call);

The bindable function can now be used to pass an array to bind as follows:

var bound = bindable(db.find, db).apply(null, arguments);

In fact you can cache bindable(db.find, db) to speed up the binding as follows:

var findable = bindable(db.find, db);
var bound = findable.apply(null, arguments);

You can use the findable function with or without an array of arguments:

var bound = findable(1, 2, 3);

Hope this helps.

查看更多
不美不萌又怎样
7楼-- · 2019-01-16 23:44

I find following cleaner than the accepted answers

Function.bind.apply(db.find, [null].concat(arguments));
查看更多
登录 后发表回答