为了账号安全,请及时绑定邮箱和手机立即绑定

在 _.invoke 源代码中,args 的作用是什么?

在 _.invoke 源代码中,args 的作用是什么?

撒科打诨 2021-12-02 10:17:31
几个月前我刚刚开始学习 JS,我试图了解“_.invoke”源代码中的“args”在做什么。请问有大佬能解答一下吗?我阅读了 mdn .apply 并阅读了其他 _.invoke 源代码,但无法理解。  _.invoke = function (collection, functionOrKey, args) {    if(typeof functionOrKey === "string") {      return _.map(collection, function(item) {        return item[functionOrKey].apply(item, args);      });    }    else return _.map(collection, function(item) {      return functionOrKey.apply(item, args);    });  };测试功能是这样的:_.invoke(['dog', 'cat'], 'toUpperCase');      });      it('runs the specified method on each item in the array, and returns a list of results', function() {        var upperCasedStrings = _.invoke(['dog', 'cat'], 'toUpperCase');        expect(upperCasedStrings).to.eql(['DOG', 'CAT']);在测试函数中,没有“参数”,为什么!?
查看完整描述

1 回答

?
HUH函数

TA贡献1836条经验 获得超4个赞

发生的所有事情是您将一个参数或参数数组应用于用于映射集合的函数 - 所有三个都是传递给函数的参数。

它类似于您可能在 Lodash/Underscore.js 等库中找到的其他方法——它本质上是一个自定义映射函数,您可以在其中传递参数,如下所示:

let mapped = _.invoke(arr, aFunc, ["anArgument", 2]);

如果没有传递参数,则传递的函数不需要参数 -toUpperCase不需要参数,所以不需要参数 - 因此没有传递。



查看完整回答
反对 回复 2021-12-02
  • 1 回答
  • 0 关注
  • 219 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信