The following code is my test code, which is important:
Copy codeThe Code is as follows:
/* Global window, jQuery, validate_email, masterUI, $, rest */
/** Enable ECMAScript "strict" operation for this function. See more:
* Http://ejohn.org/blog/ecmascript-5-strict-mode-json-and-more/
* Http://stackoverflow.com/questions/5020479/what-advantages-does-using-functionwindow-document-undefined-windo
* Q1: Why are windows and document being fed instead of just being accessed normally?
* A1: Generally to fasten the identifier resolution process, having them as local variables can help (although IMO the performance improvements may be negligile ).
* A2: Passing the global object is also a widely used technique on non-browser environments, where you don't have a window identifier at the global scope, e.g .:
* (Function (global ){
*//..
*}) (This); // this on the global execution context is the global object itself
* A3: Passing window and document allows the script to be more efficiently minified
*
* Q2: Why the heck is undefined being passed in?
* A1: This is made because the undefined global property in ECMAScript 3, is mutable, meaning that someone cocould change its value affecting your code, for example:
* Undefined = true; // mutable
* (Function (undefined ){
* Alert (typeof undefined); // "undefined", the local identifier
*}) (); // <-- No value passed, undefined by default
* If you look carefully undefined is actually not being passed (there's no argument on the function call ),
* That's one of the reliable ways to get the undefined value, without using the property window. undefined.
*
*/
(Function (window, document, undefined ){
"Use strict ";
Window. test = {
Init: function (){
"Use strict ";
Alert ("OK ");
}
};
}) (Window, document); // no undefined parameter here to avoid using mutable window. undefined changed by other guy
1. For details, refer to an article and a post on stackoverflow.
2. (function () {}) () code is written in an independent js file. When the js file is loaded by html, the function will be executed. In fact, a windows. text object is created.
Later, the html code can be called in the form of test. init.
The code for testing html is as follows:
Copy codeThe Code is as follows:
[Plain] view plaincopyprint?
<Head>
<Title> AppEngine SDK </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Script type = "text/javascript" src = ".../master/script/third_party/jquery-1.8.0.min.js"> </script>
<Script type = "text/javascript" src = ".../master/plugin/jquery-validation-1.9.0/jquery. validate. js"> </script>
<Script type = "text/javascript" src = ".../master/plugin/artDialog4.1.6/jquery. artDialog. js"> </script>
<Script type = "text/javascript" src = ".../master/script/app/test. js"> </script>
<Script type = "text/javascript">
$ (Document). ready (function (){
Test. init ();
})
</Script>
</Head>
3. Jslint will report two problems. One is about undefined and there is no good way to solve it. Let it complain. In another format, the final call method should be changed:
Copy codeThe Code is as follows:
[Javascript] view plaincopyprint ?} (Window, document);} (window, document ));
It doesn't matter. Let's use it. As long as the function is normal.