Explain the encapsulated anonymous function syntax

后端 未结 10 1615
时光说笑
时光说笑 2020-11-21 07:02

Summary

Can you explain the reasoning behind the syntax for encapsulated anonymous functions in JavaScript? Why does this work: (function(){})(); but

10条回答
  •  抹茶落季
    2020-11-21 07:17

    You can also use it like:

    ! function() { console.log('yeah') }()
    

    or

    !! function() { console.log('yeah') }()
    

    ! - negation op converts the fn definition to fn expression, therefore, you can invoke it immediately with (). Same as using 0,fn def or void fn def

提交回复
热议问题