What is the purpose of wrapping whole Javascript files in anonymous functions like “(function(){ … })()”?
问题 I have been reading a lot of Javascript lately and I have been noticing that the whole file is wrapped like the following in the .js files to be imported. (function() { ... code ... })(); What is the reason for doing this rather than a simple set of constructor functions? 回答1: It's usually to namespace (see later) and control the visibility of member functions and/or variables. Think of it like an object definition. jQuery plugins are usually written like this. In Javascript, you can nest