下面的代码是我的测试代码,注释很重要:
复制代码 代码如下:
/*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
* })(); // * 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.说明,参考了一篇文章和stackoverflow上的一个帖子
2.(function(){})() 这种代码写在独立的js文件里,当js文件被html加载的时候,该函数就会执行。实际上创建了windows.text对象。
以后html代码就可用test.init的形式调用方法。
测试html部分代码如下:
复制代码 代码如下:
[plain] view plaincopyprint?
appengine sdk
3.jslint会报两个问题,一是关于undefined的,没找到什么好方法,任它抱怨吧。另一格式最后调用方式要改成:
复制代码 代码如下:
[javascript] view plaincopyprint?}(window, document)); }(window, document));
无所谓了,就任由它吧。只要功能正常就行。