親ウィンドウサブウィンドウのjs配列をとる

19149 ワード

今このシステム、大量のセレクタと自動完成を使って、私の3寸の腐らない手によって、これらのセレクタと自動完成を最も簡略化して、道中順風満帆です.
今日の午後、ページのセレクタを補充して、複雑な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 else

30 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の2つのページがあり、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点多いです.