主题
bind
js
_.bind(func, thisArg, [partials])
创建一个函数,使用 this
绑定调用 func
,并在其接收的参数前面添加 thisArg
和 partials
。
¥Creates a function that invokes func
with the this
binding of thisArg
and partials
prepended to the arguments it receives.
_.bind.placeholder
值在单片构建中默认为 _
,可用作部分应用参数的占位符。
¥The _.bind.placeholder
value, which defaults to _
in monolithic builds, may be used as a placeholder for partially applied arguments.
注意:与原生 Function#bind
不同,此方法不设置绑定函数的 "length" 属性。
¥Note: Unlike native Function#bind
, this method doesn't set the "length" property of bound functions.
新增于
¥Since
0.1.0
参数
¥Arguments
func
(函数):要绑定的函数。¥
func
(Function): The function to bind.thisArg
(*):func
的this
绑定。¥
thisArg
()*: Thethis
binding offunc
.[partials]
(...*):要部分应用的参数。¥
[partials]
(...)*: The arguments to be partially applied.
返回
¥Returns
(函数):返回新的绑定函数。
¥(Function): Returns the new bound function.
示例
¥Example
js
function greet(greeting, punctuation) {
return greeting + ' ' + this.user + punctuation;
}
var object = { 'user': 'fred' };
var bound = _.bind(greet, object, 'hi');
bound('!');
// => 'hi fred!'
// Bound with placeholders.
var bound = _.bind(greet, object, _, '!');
bound('hi');
// => 'hi fred!'