这个胖箭如何在此代码中运行?

时间:2018-06-15 23:48:50

标签: javascript

我在这里查看此代码,并想知道箭头功能在这里是如何工作的。它与词汇的概念有关吗?

function multiplier(factor) {
  return number => number * factor;
 }

 const multiplier = (factor) => {
   return number => number * factor;
};

let twice = multiplier(2);
console.log(twice(5));

例如,我可以这样重构:

const multiplier = (factor) => number => number * factor;

let twice = multiplier(2);
console.log(twice(5));

它仍然有效。那么我在这里如何使用胖箭呢?

1 个答案:

答案 0 :(得分:0)

function multiplier(factor) {
    return number => number * factor;
}

简单等同于

function multiplier(factor) {
    return function(number) { return number * factor };
}

这里没有this