ES6 immediately invoked arrow function

后端 未结 4 1447
梦谈多话
梦谈多话 2020-11-28 02:16

Why does this work in a Node.js console (tested in 4.1.1 and 5.3.0) but doesn\'t work in the browser (tested in Chrome)? This code block should create and invok

4条回答
  •  时光取名叫无心
    2020-11-28 02:56

    I asked a question like this:

    @getify I’ve this question: to produce an #IIFE pattern we use parans around a function declaration to transform it into a function expression and then invoke it. Now in arrow function IIFEs, why do we need parans?! Isn’t the arrow function already an expression by default?!

    and this is the Kyle Simpson's answer:

    an arrow function is an expr, but we need surrounding parens b/c of "operator precedence" (sorta), so that the final parens to invoke the arrow-IIFE apply to the entire function and not to just the last token of its body.

    x => console.log(x)(4)
    

    vs

    (x => console.log(x))(4)
    

    — getify (@getify) June 12, 2020

提交回复
热议问题