[英]Prototype Function call and bind
Can someone explain logic behind 有人可以解释背后的逻辑
Function.prototype.call.bind(Array.prototype.forEach);
As I read 'bind' function give to function on which is called a context of object in which to executs. 在我阅读“ bind”函数时,将其赋予函数,该函数称为要在其中执行的对象的上下文。
But in this case bind receive function. 但是在这种情况下,绑定接收功能。 So according to me this is not intuitive syntax. 因此,根据我的说法,这不是直观的语法。 If i have further 如果我还有
myOtherFunction.call(this)
is call is still connected with context of forEach? 呼叫仍然与forEach的上下文相关联吗?
Array.prototype.forEach
is a method, bound to the context of an array. Array.prototype.forEach
是绑定到数组上下文的方法。 So you could easily iterate over an array like: 因此,您可以轻松地遍历如下数组:
[1,2,3].forEach(function(x){ console.log(x) })
This is equivalent to: 这等效于:
[].forEach.call([1,2,3],function(x){ console.log(x) })
So you are passing [1,2,3]
as the current context ( this
) of your call into forEach()
. 因此,您要将[1,2,3]
作为调用的当前上下文( this
)传递给forEach()
。
What Function.prototype.call.bind(Array.prototype.forEach);
什么Function.prototype.call.bind(Array.prototype.forEach);
does is calling ( call
) the bind
function with another function ( Array.prototype.forEach
) as its current context to a variable. 确实是使用另一个函数( Array.prototype.forEach
)作为bind
变量的当前上下文来调用( call
) bind
函数。
var forEach=Function.prototype.call.bind(Array.prototype.forEach);
means literally spoken: »If a function call on forEach
is made, please call it like you would, if you were in the context of Array.prototype.forEach
.« It is a shortcut to [].forEach.call(this,function)
意思是按字面意思说:»如果在forEach
上进行了函数调用,请在调用它时像在Array.prototype.forEach
上下文中那样调用它。«这是[].forEach.call(this,function)
的快捷方式[].forEach.call(this,function)
For further references see MDN on Function.prototype.call() , Function.protoype.bind() . 有关更多参考,请参见Function.prototype.call() , Function.protoype.bind()上的MDN。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.