[英]Function.prototype.apply.bind usages?
I perfectly know the usages for : 我非常清楚以下用法 :
Function.prototype.bind.apply(f,arguments)
Explanation - Use the original (if exists)
bind
method overf
witharguments
(which its first item will be used as context tothis
) 说明-使用原始(如果存在)bind
的方法在f
用arguments
(其它的第一项将被用作上下文this
)
This code can be used ( for example) for creating new functions via constructor function with arguments 此代码可用于(例如)通过带参数的构造函数创建新函数
Example : 示例:
function newCall(Cls) {
return new (Function.prototype.bind.apply(Cls, arguments));
}
Execution: 执行:
var s = newCall(Something, a, b, c);
But I came across this one : Function.prototype.apply.bind(f,arguments)
//word swap 但我遇到了这个: Function.prototype.apply.bind(f,arguments)
//单词交换
Question : 题 :
As it is hard to understand its meaning - in what usages/scenario would I use this code ? 因为很难理解它的含义 - 在什么用法/场景中我会使用这个代码?
This is used to fix the first parameter of .apply
. 这用于修复.apply
的第一个参数。
For example, when you get the max value from an array, you do: 例如,当您从数组中获取最大值时,您可以:
var max_value = Math.max.apply(null, [1,2,3]);
But you want to get the first parameter fixed to null
, so you could create an new function by: 但是你想把第一个参数固定为null
,所以你可以通过以下方式创建一个新函数:
var max = Function.prototype.apply.bind(Math.max, null);
then you could just do: 然后你可以这样做:
var max_value = max([1,2,3]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.