下面的代码是我的测试代码,注释很重要: 复制代码 代码如下: /*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 window 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 negligible). * 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 could 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