Strange JavaScript syntax like this: (function(){//code}) ();?

后端 未结 8 550
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-11 06:31

What does the below JavaScript mean? Why is the function embedded inside ()?

(function() {
    var b = 3;
    a += b;
}) ();
8条回答
  •  不知归路
    2020-12-11 07:05

    This are anonymous function which is executed in place.The best usage of this is to set some context or environment settings or have some side effect on load.

    This are heavily used by Ajax Toolkits like JQuery,Dojo,etc to performs load time magic and workaround many browser quirks and shortcomings.

提交回复
热议问题