Explain the encapsulated anonymous function syntax

后端 未结 10 1449
时光说笑
时光说笑 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:28

    They can be used with parameters-arguments like

    var x = 3; 
    var y = 4;
    
    (function(a,b){alert(a + b)})(x,y)
    

    would result as 7

提交回复
热议问题