如何通过对原型Array.map回调定义的方法 [英] How to pass the method defined on prototype to Array.map as callback
问题描述
我有一个数组
VAR ARR = ['A','B','C'];
和我想修剪
从每个从Array元素的空间。
这可以通过使用<一做href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map\"><$c$c>Array.map$c$c>为
arr.map(功能(EL){
返回el.trim();
});
我很好奇传递直接修剪
/ 与toLowerCase
函数的地图
回调函数,如 arr.map(Math.max.apply.bind(Math.max,NULL) ); ,以获得各子阵列或 arr.map(编号的最大元素); 投每个元素为数字。
我试过
arr.map(String.prototype.trim.apply);
但它抛出误差
未捕获类型错误:在Function.prototype.apply的呼吁未定义的,这是一个不确定的,而不是一个函数
块引用>我希望
String.prototype.trim.apply
应要求与设置为从数组元素的背景阵列中的每个元素(传递给适用
);我也试过的<不同的组合href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/apply\"><$c$c>apply$c$c>, <一href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call\"><$c$c>call$c$c>和<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind\"><$c$c>bind$c$c>没有成功。
- 为什么使用时的原型功能不能被引用
地图
- 如何函数可以作为参数传递给
地图
解决方案arr.map(String.prototype.trim.call.bind(String.prototype.trim));
呼叫
使用这个
内部,它必须指向修剪
函数来在这种情况下,正常工作。只是传递String.prototype.trim.call
将离开呼叫
未绑定任何方法,致使这个
指向窗口
来代替。值
它的工作原理,但使用时适用,而不是调用它会引发错误,
arr.map(String.prototype.trim.apply.bind(String.prototype.trim));
块引用>问题是,
地图
将通过2个参数,项目和索引。因此,最终调用类似'String.prototype.trim.apply('测试',0)
,因为第二个参数,它失败必须是一个数组。
一件事['A','B',
C。地图(String.prototype.trim.call.bind(String.prototype.toLowerCase));,
在此,我已经使用trim.call并通过与toLowerCase作为背景,然后
为什么我们需要在这里修剪,修剪为什么不叫
块引用>在使用
call.bind
您选择访问呼叫路径
函数引用变得无关紧要。将调用该函数是绑定的之一。如果你想组合功能结合在一起,您将需要一个不同的方法:
VAR通话= Function.prototype.call,
修剪= call.bind(String.prototype.trim)
与toLowerCase = call.bind(String.prototype.toLowerCase)
trimAndLowerCase = pipelineFrom(饰边,与toLowerCase);['测试'] .MAP(trimAndLowerCase);功能pipelineFrom(FN1,FN2){
复位功能(VAL){
返回FN2(FN1(VAL));
};
}不过在这一点上你有更好:
arr.map(功能(VAL){
返回val.trim()与toLowerCase()。
});I have an array
var arr = [' A ', ' b ', 'c'];
and I want to
trim
the spaces from each of the element from array.It can be done by using
Array.map
asarr.map(function(el) { return el.trim(); });
I'm curious about passing the
trim
/toLowerCase
function directly to themap
as callback function, like arr.map(Math.max.apply.bind(Math.max, null)); to get the maximum element from each subarray or arr.map(Number); to cast each element to Number.I've tried
arr.map(String.prototype.trim.apply);
but it is throwing error
Uncaught TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function
I expect that
String.prototype.trim.apply
should be called for each element in the array with the context set to the element from array(passed toapply
);I've also tried different combinations of
apply
,call
andbind
with no success.
- Why the function on prototype cannot be referenced when using
map
- How function can be passed as parameter to
map
解决方案arr.map(String.prototype.trim.call.bind(String.prototype.trim));
call
usesthis
internally, which must point to thetrim
function to work properly in this case. Simply passingString.prototype.trim.call
would leavecall
unbound to any method, resulting in thethis
value pointing towindow
instead.It works, but when used apply instead of call it throws error, arr.map(String.prototype.trim.apply.bind(String.prototype.trim));
The problem is that
map
will pass 2 arguments, the item and the index. Therefore it ends up calling something like'String.prototype.trim.apply('test', 0)
which fails since the second argument must be an array.one more thing [' A ', ' B ', 'c'].map(String.prototype.trim.call.bind(String.prototype.toLowerCase));, in this, I've used trim.call and passed toLowerCase as context then why we need trim here, why trim is not called
When using
call.bind
the path that you chose to access thecall
function reference becomes irrelevant. The function that will get called is the one that is bound.If you want to compose functions together you will need a different approach:
var call = Function.prototype.call, trim = call.bind(String.prototype.trim), toLowerCase = call.bind(String.prototype.toLowerCase), trimAndLowerCase = pipelineFrom(trim, toLowerCase); [' TeST '].map(trimAndLowerCase); function pipelineFrom(fn1, fn2) { return function (val) { return fn2(fn1(val)); }; }
However at this point you're better off with:
arr.map(function (val) { return val.trim().toLowerCase(); });
这篇关于如何通过对原型Array.map回调定义的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!