主题
bindKey
js
_.bindKey(object, key, [partials])
创建一个调用 object[key]
处的方法的函数,并在其接收的参数前面添加 partials
。
¥Creates a function that invokes the method at object[key]
with partials
prepended to the arguments it receives.
此方法与 _.bind
不同,它允许绑定函数引用可能被重新定义或尚不存在的方法。有关更多详细信息,请参阅 Peter Michaux 的文章。
¥This method differs from _.bind
by allowing bound functions to reference methods that may be redefined or don't yet exist. See Peter Michaux's article for more details.
_.bindKey.placeholder
值在单片构建中默认为 _
,可用作部分应用参数的占位符。
¥The _.bindKey.placeholder
value, which defaults to _
in monolithic builds, may be used as a placeholder for partially applied arguments.
新增于
¥Since
0.10.0
参数
¥Arguments
object
(对象):要在其上调用方法的对象。¥
object
(Object): The object to invoke the method on.key
(字符串):方法的键。¥
key
(string): The key of the method.[partials]
(...*):要部分应用的参数。¥
[partials]
(...)*: The arguments to be partially applied.
返回
¥Returns
(函数):返回新的绑定函数。
¥(Function): Returns the new bound function.
示例
¥Example
js
var object = {
'user': 'fred',
'greet': function(greeting, punctuation) {
return greeting + ' ' + this.user + punctuation;
}
};
var bound = _.bindKey(object, 'greet', 'hi');
bound('!');
// => 'hi fred!'
object.greet = function(greeting, punctuation) {
return greeting + 'ya ' + this.user + punctuation;
};
bound('!');
// => 'hiya fred!'
// Bound with placeholders.
var bound = _.bindKey(object, 'greet', _, '!');
bound('hi');
// => 'hiya fred!'