如何通过对原型Array.map回调定义的方法 [英] How to pass the method defined on prototype to Array.map as callback

查看:225
本文介绍了如何通过对原型Array.map回调定义的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组

  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

  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, <一href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/call\"><$c$c>call和<一个href=\"https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function/bind\"><$c$c>bind没有成功。


  1. 为什么使用时的原型功能不能被引用地图

  2. 如何函数可以作为参数传递给地图


解决方案

  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 as

arr.map(function(el) {
    return el.trim();
});

I'm curious about passing the trim/toLowerCase function directly to the map 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 to apply);

I've also tried different combinations of apply, call and bind with no success.

  1. Why the function on prototype cannot be referenced when using map
  2. How function can be passed as parameter to map

解决方案

arr.map(String.prototype.trim.call.bind(String.prototype.trim));

call uses this internally, which must point to the trim function to work properly in this case. Simply passing String.prototype.trim.call would leave call unbound to any method, resulting in the this value pointing to window 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 the call 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆