有没有办法在箭头函数声明后立即调用它?

Is there a way to call an Arrow Function just after it has been declared?

有时我想将箭头函数的结果赋给一个变量,但是如果我将箭头函数赋给那个变量,它得到的是函数而不是那个函数的 return,这是显而易见的。

let theNumber2 = ()=>{return 2};

theNumber2 将包含函数,只有当我这样做时 theNumber2() 我才会得到 returned 2.

有没有办法像这样直接得到箭头函数的return?例如:

let theNumber2 = ()=>{return 2}(); // Note the () at the final

有没有办法做到这一点?我的意思是,分配调用的箭头函数?

只需用括号括起来并调用该函数。

let theNumber2 = (() => { return 2; })();

console.log(theNumber2);