可能重复:
Why would one write global code inside a function definition-call pair?
How does the (function() {})() construct work and why do people use it?
我不是JS忍者,现在我正在努力学习一种比我想象的更深入的语言的来龙去脉!
我最近看过很多代码(其中有jquery),编码如下:
(function(){
var myLibrary = .....;
window.myLibrary = myLibrary;
})();
有人可以向我解释一下这种语法的好处吗?它之间有什么区别,只是写下这样的东西:
var myLibrary = {
...
}