2011-11-10 65 views
0
if (_this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"][imageNo] != undefined) { 
    if (imageNo > (_this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"].length - 1)) { 
     imageNo = 0; 
     _this.categoriesCurrentImage[categoryIndex] = imageNo; 
    } 
    var img = _this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"][imageNo]; 

    _this.showImage(img, divId); 
    _this.categoriesCurrentImage[categoryIndex]++; 
    _this.categoryIndex++; 
} else { 

    if (imageNo > (_this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"].length - 1)) { 
     imageNo = 0; 
     _this.categoriesCurrentImage[categoryIndex] = imageNo; 
    } 

    var img = _this.tiles["suz:SuzukiHubResponse"]["suz:categories"][category][aspect]["suz:image"]; 
    _this.showImage(img, divId); 
    _this.categoriesCurrentImage[categoryIndex]++; 
    _this.categoryIndex++; 
} 

誰能告訴我爲什麼。如果語句是真的,代碼去其他地方?

我最終在else子句中,即使if爲真? 這對我來說絕對沒有意義。

如果在suz:image節點下面有超過1個圖像,它應該落入else中。它有99%的時間。

+2

你的代碼格式不。解決這個問題,這個錯誤應該變得更加明顯。 – zzzzBov

+2

使用一些更多變量來使代碼更易於閱讀和調試。例如,把var img = _this.tiles [「suz:SuzukiHubResponse」] [「suz:categories」] [category] ​​[aspect] [「suz:image」];更高,並在適當的測試中使用它。 –

回答

0

你需要在引號包裹undefined

!= "undefined" 
+3

將未定義的引號放在引號中並不是必需的方式。當它是'typeof'的結果時,它只需要在引號中。否則,它不包含引號。有關更多信息,請參閱https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined。 – jfriend00

+0

有人可以向我解釋爲什麼把這一切都放在一個變量中做的伎倆?我把大部分的json結構放入一個名爲img的變量中。現在它可以工作。 – Puzzle84