javascript 尝试将普通函数重构为箭头函数[重复]

ws51t4hk  于 2023-09-29  发布在  Java
关注(0)|答案(1)|浏览(108)

此问题已在此处有答案

Why is my arrow function returning undefined?(2个答案)
18小时前关闭
我正在尝试将我的乘法函数重构为JS中的箭头函数。使用普通函数时它工作得很好,但从下面的箭头函数来看,当我传入参数时它就不工作了。我做错了什么?

  1. // Original code
  2. function multiply(a, b) {
  3. return a * b;
  4. }
  5. // Refactored code using arrow function
  6. const multiply = (a, b) => { a * b };
nwsw7zdq

nwsw7zdq1#

添加return语句:

  1. // add a return statement :
  2. const multiply = (a, b) => { return a * b };

或删除大括号:

  1. // remove the curly :
  2. const multiply = (a, b) => a * b;

相关问题