如何将原型上定义的方法作为回调传递给 Array.map [英] How to pass the method defined on prototype to Array.map as callback

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

问题描述

我有一个数组

var arr = [' A ', ' b ', 'c'];

我想修剪数组中每个元素的空格.

and I want to trim the spaces from each of the element from array.

可以通过使用Array.map as

It can be done by using Array.map as

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

我很好奇将 trim/toLowerCase 函数作为回调函数直接传递给 map,例如 arr.map(Math.max.apply.bind(Math.max, null)); 获取每个子数组的最大元素或arr.map(Number); 将每个元素转换为 Number.

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.

我试过了

arr.map(String.prototype.trim.apply);

但它抛出错误

Uncaught TypeError: Function.prototype.apply 在 undefined 上被调用,这是一个 undefined 而不是函数

Uncaught TypeError: Function.prototype.apply was called on undefined, which is a undefined and not a function

我希望 String.prototype.trim.apply 应该为数组中的每个元素调用,上下文设置为数组中的元素(传递给 apply);

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);

我也尝试了 申请, callbind 没有成功.

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

  1. 为什么在使用map
  2. 时无法引用prototype上的函数
  3. 如何函数可以作为参数传递给map
  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 在内部使用 this,它必须指向 trim 函数才能在这种情况下正常工作.简单地传递 String.prototype.trim.call 将使 call 未绑定到任何方法,导致 this 值指向 window 代替.

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.

它有效,但是当使用 apply 而不是 call 时会抛出错误,arr.map(String.prototype.trim.apply.bind(String.prototype.trim));

It works, but when used apply instead of call it throws error, arr.map(String.prototype.trim.apply.bind(String.prototype.trim));

问题在于 map 将传递 2 个参数,项目和索引.因此它最终调用了类似 'String.prototype.trim.apply('test', 0) 的东西,它失败了,因为第二个参数必须是一个数组.

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.

还有一件事 [' A ', ' B ','c'].map(String.prototype.trim.call.bind(String.prototype.toLowerCase));,在这方面,我使用了 trim.call 并将 toLowerCase 作为上下文传递然后为什么这里需要trim,为什么不叫trim

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

当使用 call.bind 时,您选择访问 call 函数引用的路径变得无关紧要.将被调用的函数是被绑定的函数.

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天全站免登陆