標籤:style blog color io ar strong for 資料 art
現在這個系統,用到了大量的選取器 和 自動完成,憑藉我的三寸不爛之手, 將這些選取器 和 自動完成做到了最簡化, 一路順風順水.
今天下午補充一個頁面的選取器, 要取一個複雜的 json 對像,用來填充資料.
這個 json 對象包含數組, 取出來後,要轉變為本地頁面(選取器的父頁面)的某個"類"的執行個體.
我用這個方法來將 json 對象轉換為某個"類"的對象:
1 _.automap = function (data, type, writeNotExistsProperty, maps, callback, propertyWrap) { 2 /// <summary> 3 /// 將 Json 對象轉換為目標類型的對象 4 /// </summary> 5 /// <param name="data">資料</param> 6 /// <param name="type">目標類型</param> 7 /// <param name="writeNotExistsProperty">如果目標類型中不存在對應的屬性,是否寫入,預設不寫入</param> 8 /// <param name="maps">屬性對應列表,如{PropertyA:‘PA‘,PropertyB:‘B‘,...}</param> 9 /// <returns type="">目標類型的資料,如果原資料是數組,結果目標類型的數組</returns>10 if (data == null)11 return null;12 13 if (data instanceof Array) {14 var item, result = [];15 for (var i = 0; item = data[i]; i++) {16 result.push(automap(item, type, writeNotExistsProperty, maps, callback, propertyWrap));17 }18 return result;19 } else if (data instanceof Object) {20 var result = new type();21 for (var k in data) {22 var targetProperty = k;23 var value = data[k];24 25 if (maps != null) {26 if (typeof (maps[k]) == "string") {27 if (typeof (propertyWrap) == "function")28 targetProperty = propertyWrap(maps[k])29 else30 targetProperty = maps[k];31 } else if (typeof (maps[k]) == "function") {32 value = automap(value, maps[k], msWriteProfilerMark, callback, propertyWrap)33 }34 } else if (typeof (propertyWrap) == "function") {35 value = propertyWrap(value);36 }37 38 //不嚴格等於39 if (result[targetProperty] !== undefined || writeNotExistsProperty) {40 result[targetProperty] = value;41 }42 }43 if (callback instanceof Function) {44 callback(result, data);45 }46 return result;47 }48 }
結果運後,發現跟本就沒有實際的執行, 跟蹤了一下,發現問題出現在 data instanceof Array 這個判斷上.
傳進來的,"我認為" 明明是個數組, 結果卻是個 object , 很是鬱悶.
可能各位 js 高手不以為然, 這裡我寫個簡單的例子,還原一下情境,找出問題.
有 A / B 兩個頁面, B類比我的選取器,也就是子視窗.
A.html
1 <html> 2 <body> 3 <iframe src="b.html" id="b"></iframe> 4 <script> 5 var a = [1,2,3] 6 alert("A.html " + typeof(a) + " " + (a instanceof Array)) 7 8 function alertSub(o){ 9 alert("alertSub without window " + typeof(o) + " " + (o instanceof Array)) ;10 }11 12 function alertSubWithWindow(w,o){13 alert("alertSub With window " + typeof(o) + " " + (o instanceof w.Array)) ;14 }15 16 function alertSubVarType(o){17 alert(typeof(o));18 }19 </script>20 </body>21 </html>
b.html
1 <html> 2 <body> 3 <script> 4 var a = new Array(1,2,3); 5 var b = "aaa"; 6 var c = new Date(); 7 var d = 100; 8 var e = false; 9 window.parent.alertSub(a);10 window.parent.alertSubWithWindow(window,a);11 window.parent.alertSubVarType(b);12 window.parent.alertSubVarType(c);13 window.parent.alertSubVarType(d);14 window.parent.alertSubVarType(e);15 </script>16 </body>17 </html>
b.html 什麼也不做,就是聲明幾個變數, 調用父視窗(A.html) 裡的方法.
b.html 裡有個 var a = new Array(1,2,3), 通過 window.parent.alertSub(a) 在父頁面裡用 xxx instanceof Array 來判斷它是不是個 Array
猜猜會是什麼結果? 聲明的是 new Array噢!
彈出的是什麼? 是 false !
蛋疼了一會.
想了一下,發現了一個忽略的錯誤.
Array/ Date 這些內建對象都是在 window 對象下面的.
我們聲明 Array 一般不寫成 new window.Array, 是因為當前頁面的範圍就是 window.
這是個常識,但很容易被忽略.
在做個測試:
window.parent.alertSubWithWindow(window,a);
...
function alertSubWithWindow(w,o){
alert("alertSub With window " + typeof(o) + " " + (o instanceof w.Array)) ;
}
這次是 true 了.
改一下選取器, 原來是這樣寫的:
var data = $.parseJSON($(this).attr("data-port-info"));
現在改成:
var data = window.parent.$.parseJSON($(this).attr("data-port-info"));
即用父頁面的 $.parseJSON 來解析這個 json ,然後傳給父頁面.
-----------------------------------------------------------------------------------------------------
CNM!GSQ
很久很久以前,有個人事給我說,升職考核的是 綜合能力
我來理解一下這個綜合能力:
我:
能力70 + 拍馬屁10 共 80
他(泛指,請不要對號入座,如果對號也可以):
能力40 + 拍馬屁 + 50 共 90
還比我多 10分.
父視窗取子視窗的 js 數組