【发布时间】:2017-10-11 15:14:31
【问题描述】:
是否可以重新绑定一个函数,使其绑定到原来的同一个对象,但接受不同的参数值?
比如……
// Object definition
function OriginalOwner(prop) { this.prop = prop; }
OriginalOwner.prototype.aFunc = function(arg) { return this.prop + arg; }
// Object instance
var owner = new OriginalOwner("Returned ");
// Example function bindings
var boundFunc = owner.aFunc.bind(owner);
var reboundFunc = boundFunc.bind(boundFunc.owner, "myArgument");
// Calling rebound function
console.log(reboundFunc()); // outputs "Returned myArgument"
【问题讨论】:
-
这段代码有效,不是吗?
-
@Bergi
boundFunc上没有所有者属性,但无意中这仍然有效 -
是的 - 你可以传递任何你想要的东西(并且可能应该传递
null是明确的),因为你不能重新绑定一个绑定的函数。见stackoverflow.com/q/26545549/1048572 或stackoverflow.com/q/7282158/1048572
标签: javascript