First, let's take a look at this:
(Function (){
// RunCode
})();
The red brackets are an anonymous function, and the red brackets are separated, indicating that the function is a part. The green brackets indicate an operator, indicating that the function in the red brackets is to be run.
It is equivalent to defining an anonymous function and letting it run directly.
To build your own JS library, use the following template code:
(Function (){
Function $ (){
// Code
}
Window ['mynamespace'] ={} // define your own namespace // window ['ic'] = {}
Window ['mynamespace'] ['$'] = $; // register your $ function to the window namespace.
})();
Reference the functions in your own library on the page as follows:
<Body onload = "ic. $ ()"> </body> // IC is the defined namespace, and you can call the function in your own JS library file.
You can also write <body onload ="Window.IC. $ () "> </body> // window can be omitted.
Original article: http://hi.baidu.com/mutourenoo/blog/item/6cd36ac4fa1419a28326ac4c.html