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

JavaScript 箭头函数中的问题

JavaScript 箭头函数中的问题

SMILET 2021-12-23 18:06:42
我是新手我已经在此链接中下载了一些源 https://github.com/the-road-to-learn-react/react-redux-example 我在文件 src/app.js 第 4 行中有一些问题const applyFilter = searchTerm => article =>  article.title.toLowerCase().includes(searchTerm.toLowerCase());为什么这不能与const applyFilter = searchTerm => article =>{  article.title.toLowerCase().includes(searchTerm.toLowerCase());} 或者const applyFilter = searchTerm => {article =>  article.title.toLowerCase().includes(searchTerm.toLowerCase());}并在第 14 行调用函数时articles.filter(applyFilter(searchTerm))const applyFilter = searchTerm => article =>  article.title.toLowerCase().includes(searchTerm.toLowerCase());这在我看来是在箭头函数内调用箭头函数?他们怎么能把 var 'article' 放进去?
查看完整描述

1 回答

?
LEATH

TA贡献1936条经验 获得超7个赞

箭头函数语法有点像这样


(a) => b(a);


is equivalent to


function(a) {

    return b(a);

}

但是,当您添加{}到箭头函数时,它会改变含义:


(a) => { b(a); }


is equivalent to


function(a) {

    b(a); 

    // notice that this doesn't actually return anything, 

    // it just calls a function and does nothing with the result

}

所以加括号的时候要加return语句


(a) => { return b(a); }


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

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号