jquery Official document on the recommended development plug-in to the following format to write, I first read the source code when I see this way, was depressed long time.
(function ($) {
Code goes here
}) (JQuery);
The following will record why this is written, and what are the specific benefits of writing this.
In fact, the above method is equivalent to the following definition, but the above method is more secure. Similar to the hidden class in Java, after the definition, a large program to avoid third-party misoperation, thereby destroying the encapsulation.
The above code is equivalent to the following code, I would like to write that everyone can see it.
var jqueryfucntion=function ($) {
Code goes here
//
}
Jqueryfucntion (JQuery);
(function ($) {
Code goes here
})
A jquery function is defined above, and the formal parameter is $
(function ($) {
Code goes here
}) (JQuery);
This is called the previously defined function, and then the jquery argument is passed in.
The advantage of this is that when we write a jquery plugin, we can also use this alias, without causing a conflict with prototype.
If it is not defined in this way, and end-use uses prototype, then there will be irreconcilable contradictions.
jquery notes-Plugin development tips