javascript全域變數封裝模組實現代碼

來源:互聯網
上載者:User

下面的代碼是我的測試代碼,注釋很重要: 複製代碼 代碼如下:/*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

1.說明,參考了一篇文章和stackoverflow上的一個文章
2.(function(){})() 這種代碼寫在獨立的js檔案裡,當js檔案被html載入的時候,該函數就會執行。實際上建立了windows.text對象。
以後html代碼就可用test.init的形式調用方法。
測試html部分代碼如下: 複製代碼 代碼如下:[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會報兩個問題,一是關於undefined的,沒找到什麼好方法,任它抱怨吧。另一格式最後調用方式要改成: 複製代碼 代碼如下:[javascript] view plaincopyprint?}(window, document)); }(window, document));

無所謂了,就任由它吧。只要功能正常就行。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.