Is there any reason to wrap anonymous JavaScript functions in braces?

前端 未结 3 2033
轻奢々
轻奢々 2020-12-19 15:02
var a = function () {
    return \'test\';
}();

console.log(a);

Answer in First Case : test

var a = (function () {
    return \'te         


        
3条回答
  •  我在风中等你
    2020-12-19 15:18

    It is good practice (but not required) to wrap IIFEs (Immediately Invoked Function Expressions) in parenthesis for readability. If your function is long and the reader cannot see the end, the opening parenthesis calls the readers attention to the fact that there is something special about this function expression and forces them to look at the bottom to find out what it is.

提交回复
热议问题