1. this
UISmartMonkey.prototype.RELEASE_SMARTMONKEY = function() {
// ...
// 設(shè)定定時任務(wù)
var t=this;
setInterval(10000, function() {
if (t.getPageName() != t.commonData.pageName.liveHome && t.getPageName() != t.commonData.pageName.liveDetail) {
t.openPageWithUrl(t.commonData.pageUrl.liveHome);
}
});
// ...
}
UISmartMonkey.prototype.getPageName = function() {
// ...
}
UISmartMonkey.prototype.openPageWithUrl = function(url) {
// ...
}
- 這里來解釋一下,為何需要
var t=this;
- 在全局函數(shù)中,this等于window;在某個對象的函數(shù)中,this等于這個對象;在匿名函數(shù)中,this指向的也是window;
- 上面setInterval中的function()是個匿名函數(shù),如果直接調(diào)用this.getPageName(),會報錯:Can't find variable: this;
- 將this賦值給t,這樣就可以在function中調(diào)用t訪問到當(dāng)前實例
- 具體見《JS高程》P182
2. || {}
var DYUI = DYUI || {};
- 如果DYUI是null或者undefined(其實還有0,"",false這三種情況),則將DYUI初始化為空對象
- 有種 ifndef define endif 的感覺
3. 立即執(zhí)行函數(shù)
(function() {
DYUI.namespace = function() {
// ...
};
})();
- 這是個立即執(zhí)行函數(shù)
- 函數(shù)定義外面的一層括號起到將函數(shù)聲明轉(zhuǎn)為函數(shù)表達(dá)式,因為只有函數(shù)表達(dá)式才可以使用括號調(diào)用
- 后面那個括號就是表示調(diào)用
4. namespace
(function() {
DYUI.namespace = function() {
var a = arguments, o = null, i, j, d;
for (i = 0; i < a.length; i = i + 1) {
d = ("" + a[i]).split(".");
o = DYUI;
for (j = (d[0] == "DYUI") ? 1 : 0; j < d.length; j = j + 1) {
o[d[j]] = o[d[j]] || {};
o = o[d[j]];
}
}
return o;
};
DYUI.namespace('consts');
DYUI.consts.OP = Object.prototype;
DYUI.consts.FUNCTION_TOSTRING = '[object Function]';
DYUI.consts.ARRAY_TOSTRING = '[object Array]';
// ...
}
- 這是參考了YUI(Yahoo User Interface Library)中對于namespace的寫法,簡單來說,定義了這個namespace之后,就可以用
DYUI.namespace('consts')來申明一個consts的命名空間
5. Object.prototype.toString.apply()
DYUI.consts.OP = Object.prototype;
DYUI.consts.FUNCTION_TOSTRING = '[object Function]';
DYUI.consts.ARRAY_TOSTRING = '[object Array]';
isFunction: function(o) {
return (typeof o === 'function') || DYUI.consts.OP.toString.apply(o) === DYUI.consts.FUNCTION_TOSTRING;
},
- js中檢測類型有這么幾種辦法:
- typeof
- 用法:
alert(typeof arr);
- 缺點:只能區(qū)分基本類型number,string,undefined,boolean,object五種
- instanceof
- 用法:
alert(person instanceof Array);
- 缺點:只能被動是去判斷是不是某類型,但是不能主動知道類型
- Object.prototype.toString.apply() / Object.prototype.toString.call()
function isFunction(it) {
return Object.prototype.toString.apply(it) === '[object Function]';
}
6. &&
this.index == oBtn.length - 1 && (oDiv.style.cssText = "");
- 這段代碼一開始沒看懂,原因是對&&的語法不熟悉
- 對于
a && b js會順序執(zhí)行,先判斷a是否為true,若是則繼續(xù)執(zhí)行b;若不是則不再執(zhí)行,于是上面這行代碼可以改成如下寫法:
if (this.index == oBtn.length - 1) {
oDiv.style.cssText = "";
}
- 于是這句就可以理解了,它實現(xiàn)的是重置功能,即將原來加的css樣式都清空
-
7. 函數(shù)聲明不能放在 $(document).ready()里面
// <script>
$(document).ready(function(){
function uploadAndSubmit() {
//
}
});
// <body>
<form name="demoForm" id="demoForm" method="post" enctype="multipart/form-data" action="javascript: uploadAndSubmit();">
</form>
- 報錯:Uncaught ReferenceError: uploadAndSubmit is not defined
- 原因是 函數(shù)申明放在
$(document).ready 里面,這樣頁面加載時還拿不到這個函數(shù),于是form無法綁定 uploadAndSubmit 這個函數(shù)
- 解決方法是:將函數(shù)定義放到
$(document).ready 外面
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。