Difference between using void vs wrapping in parens for IIFE void function() vs (function())

前端 未结 1 1145
抹茶落季
抹茶落季 2021-02-15 20:38

The common practise for creating modules is to wrap them in parens so you won\'t leak any variables outside of the module (when concatenating etc).

There is also

1条回答
  •  情歌与酒
    2021-02-15 21:05

    Well, many JavaScript programmers think void is confusing and redundant, especially Douglas Crockford who calls it one of the "Bad Parts" of JavaScript.

    Preceding a function definition with void can be especially confusing. In languages like C++, it means "This is a type of function that doesn't return a value." In JavaScript, void doesn't define anything; instead it evaluates the function (or other expression) and returns the value undefined. So you don't see it much in JavaScript code.

    For more info on using ! to precede modules, check out this StackOverflow answer.

    Also make sure to read Ben Allman's original blog post on IIFE's.

    0 讨论(0)
提交回复
热议问题