在 Javascript 中,如何在不绑定this
参数的情况下将参数绑定到函数?
例如:
//Example function.
var c = function(a, b, c, callback) {};
//Bind values 1, 2, and 3 to a, b, and c, leave callback unbound.
var b = c.bind(null, 1, 2, 3); //How can I do this without binding scope?
this
我怎样才能避免必须绑定函数的范围(例如设置= null)的副作用?
编辑:
对困惑感到抱歉。我想绑定参数,然后能够稍后调用绑定函数并让它的行为就像我调用原始函数并将绑定参数传递给它一样:
var x = 'outside object';
var obj = {
x: 'inside object',
c: function(a, b, c, callback) {
console.log(this.x);
}
};
var b = obj.c.bind(null, 1, 2, 3);
//These should both have exact same output.
obj.c(1, 2, 3, function(){});
b(function(){});
//The following works, but I was hoping there was a better way:
var b = obj.c.bind(obj, 1, 2, 3); //Anyway to make it work without typing obj twice?
我还是新手,很抱歉造成混乱。
谢谢!