如何将箭头函数转换为常规函数

如何将箭头函数转换为常规函数

我这里有这个示例,我想将 Arrow 函数转换为普通函数,但不知道如何操作。谢谢

const intialValue = 0
const lineItems = [
  { description: 'Eggs (Dozen)', quantity: 1, price: 3, total: 3 },
  { description: 'Cheese', quantity: 0.5, price: 5, total: 2.5 },
  { description: 'Butter', quantity: 2, price: 6, total: 12 }
];
const tt = lineItems.reduce((sum, li) => sum + li.total, intialValue)
console.log(tt)

答案1

要将箭头函数转换为非箭头函数,需要给它命名:

function mysumfunc (sum, li) {
    return sum + li.total;
};

为了更好地理解 JavaScript 减少,请看这个更明确的例子:

const array1 = [1, 2, 3, 4];

// 0 + 1 + 2 + 3 + 4
const initialValue = 0;
const sumWithInitial = array1.reduce(
  (previousValue, currentValue) => previousValue + currentValue,
  initialValue
);

相关内容