绑定和可调用模式有什么用途? [英] What are the uses for the bindable and callable pattern?
问题描述
我已经看过这段小小的代码,并且从未真正花时间围绕它做什么。
var bind = Function.bind;
var call = Function.call;
var bindable = bind.bind(bind);
var callable = bindable(call);
我在理念和实践中理解 .call
do,但创建可绑定
和<$的好处,优点或实际用途c $ c> callbable 上面的函数
下面是一个bindable用例的上下文示例。
var bound = bindable(db.find,db).apply(null,arguments);
var findable = bindable(db.find,db);
var bound = findable.apply(null,arguments);
var bound = findable(1,2,3);
这种模式可用于什么?
$
bind.bind(呼叫);
执行摘要是 callable
函数可以让你在一些 prototype
对象上使用一个被设计用作函数的函数,并创建一个新函数接受一个参数作为<$ c $的值例如,考虑 .slice()
方法。
参数
结构转换为数组。这通常是这样做的: var args = [] .slice.call(arguments,0);
那么 callable
函数可以让我们创建一个独立功能,可以起到类似的作用:
var cslice = callable(Array.prototype.slice);
var args = cslice(arguments,0);
现在,无论是有用还是不是某种类型的基石编程风格,我不会说。
I've seen this little snippet of code floating around before and never really taken the time to wrap my head around what it does.
var bind = Function.bind;
var call = Function.call;
var bindable = bind.bind(bind);
var callable = bindable(call);
I understand in concept and in practice what .bind
and .call
do, but what is the benefit, advantage or practical use of creating the bindable
and callbable
functions above?
Below is a contextual example of a use case for bindable.
var bound = bindable(db.find, db).apply(null, arguments);
var findable = bindable(db.find, db);
var bound = findable.apply(null, arguments);
var bound = findable(1, 2, 3);
What can this pattern be used for?
Well callable
is effectively the result of
bind.bind(call);
The "executive" summary is that the callable
function lets you take a function designed to operate as a function on some prototype
object and create a new function that accepts a parameter to be used as the value of this
.
As an example, consider the .slice()
method on the Array prototype. It's often used to transform things like a node list or an arguments
structure into an array. That's generally done like this:
var args = [].slice.call(arguments, 0);
Well the callable
function lets us create a stand-alone function that'll work similarly:
var cslice = callable(Array.prototype.slice);
var args = cslice(arguments, 0);
Now, whether this is useful or not as a cornerstone of some kind of programming style, I won't say.
这篇关于绑定和可调用模式有什么用途?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!