javascript全局變量封裝模塊實(shí)現(xiàn)代碼
/*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.說(shuō)明,參考了一篇文章和stackoverflow上的一個(gè)帖子
2.(function(){})() 這種代碼寫在獨(dú)立的js文件里,當(dāng)js文件被html加載的時(shí)候,該函數(shù)就會(huì)執(zhí)行。實(shí)際上創(chuàng)建了windows.text對(duì)象。
以后html代碼就可用test.init的形式調(diào)用方法。
測(cè)試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會(huì)報(bào)兩個(gè)問(wèn)題,一是關(guān)于undefined的,沒(méi)找到什么好方法,任它抱怨吧。另一格式最后調(diào)用方式要改成:
[javascript] view plaincopyprint?}(window, document)); }(window, document));
無(wú)所謂了,就任由它吧。只要功能正常就行。
- 基于JavaScript 聲明全局變量的三種方式詳解
- javascript中局部變量和全局變量的區(qū)別詳解
- Javascript全局變量var與不var的區(qū)別深入解析
- javascript中運(yùn)用閉包和自執(zhí)行函數(shù)解決大量的全局變量問(wèn)題
- JavaScript中全局變量、函數(shù)內(nèi)變量以及常量表達(dá)式的效率測(cè)試
- 理運(yùn)用命名空間讓js不產(chǎn)生沖突避免全局變量的泛濫
- 淺談JavaScript的全局變量與局部變量
- JS全局變量和局部變量最新解析
- js隱式全局變量造成的bug示例代碼
- Javascript學(xué)習(xí)之談?wù)凧S的全局變量跟局部變量(推薦)
- 探討JavaScript中聲明全局變量三種方式的異同
- JavaScript防止全局變量污染的方法總結(jié)
相關(guān)文章
js實(shí)現(xiàn)倒計(jì)時(shí)器自定義時(shí)間和暫停
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)倒計(jì)時(shí)器自定義時(shí)間和暫停,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-02-02js實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名系統(tǒng)
這篇文章主要介紹了js實(shí)現(xiàn)課堂隨機(jī)點(diǎn)名系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-11-11javascript實(shí)現(xiàn)校驗(yàn)文件上傳控件實(shí)例
這篇文章主要介紹了javascript實(shí)現(xiàn)校驗(yàn)文件上傳控件,實(shí)例分析了javascript檢測(cè)上傳文件類型是否為圖片的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04JavaScript基礎(chǔ)進(jìn)階之?dāng)?shù)組方法總結(jié)(推薦)
下面小編就為大家?guī)?lái)一篇JavaScript基礎(chǔ)進(jìn)階之?dāng)?shù)組方法總結(jié)(推薦)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09ie9 提示''console'' 未定義問(wèn)題的解決方法
關(guān)掉開(kāi)發(fā)者工具之后,在狀態(tài)欄發(fā)現(xiàn)提示'console' 未定義,為什么之前的運(yùn)行沒(méi)有問(wèn)題,之后的就不行呢2014-03-03詳解解決小程序中webview頁(yè)面多層history返回問(wèn)題
這篇文章主要介紹了詳解解決小程序中webview頁(yè)面多層history返回問(wèn)題,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08js實(shí)現(xiàn)圖片旋轉(zhuǎn) js滾動(dòng)鼠標(biāo)中間對(duì)圖片放大縮小
這篇文章主要為大家詳細(xì)介紹了js實(shí)現(xiàn)圖片旋轉(zhuǎn),滾動(dòng)鼠標(biāo)中間對(duì)圖片放大縮小等效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-07-07后端代碼規(guī)范避免數(shù)組下標(biāo)越界
這篇文章主要為大家介紹了后端開(kāi)發(fā)中的代碼如何規(guī)范避免數(shù)組下標(biāo)越界示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06讓iframe自適應(yīng)高度(支持XHTML,支持FF)
讓iframe自適應(yīng)高度(支持XHTML,支持FF)...2007-07-07js通過(guò)audioContext實(shí)現(xiàn)3D音效
這篇文章主要為大家詳細(xì)介紹了js通過(guò)audioContext實(shí)現(xiàn)3D音效,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04