网页参数绑定
One of my favorite and most essential Function
method is bind
, a function we added to MooTools when it wasn't featured in the JavaScript language itself. We often think of using bind
to simply bind a method's call to its host object, but did you know you can also bind arguments with the host object?
我最喜欢的也是最基本的Function
方法之一是bind
,这是我们在JavaScript语言本身未提供的功能时添加到MooTools的函数。 我们经常想到使用bind
来简单地将方法的调用绑定到其宿主对象,但是您知道您也可以将参数与宿主对象绑定吗?
You've probably done something like this:
您可能已经做过这样的事情:
this._onTargetAvailable = this._onTargetAvailable.bind(this);
That pattern is frequently used, especially in classed-based code or when passing callback functions. What you may not often see is bound arguments:
经常使用该模式,尤其是在基于分类的代码中或在传递回调函数时。 您可能不会经常看到的是绑定参数:
this._onTargetAvailable = this._onTargetAvailable.bind(
this,
arg1,
arg2,
arg3
);
Binding arguments gives you more power in how your bound function is used! Whenever onTargetAvailable
is called, the arguments you provide will be in that order, and any additional arguments will be added to the end of the argument list!
绑定参数使您可以更有效地使用绑定函数! 每当onTargetAvailable
,您提供的参数将按照该顺序排列,并且任何其他参数都将添加到参数列表的末尾!
网页参数绑定