2013-03-08 71 views
0

我有以下的javascript代碼塊,而且也不是很清楚的:請解釋這行js代碼。

var level = this.getLevelForResolution(this.map.getResolution()); 
var coef = 360/Math.pow(2, level); 

var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX)/coef) : Math.round((this.topTileFromX - bounds.right)/coef); 
var y_num = this.topTileFromY < this.topTileToY ? Math.round((bounds.bottom - this.topTileFromY)/coef) : Math.round((this.topTileFromY - bounds.top)/coef); 

是什麼在this.topTileFromX <<是什麼意思?

+1

「<」是小於三元運算符條件的符號。除此之外,這個問題不是很清楚。 – 2013-03-08 06:15:18

+1

@TimMedora,我認爲他用英語表達有困難。我編輯了這個問題,使其更加清晰。 – saji89 2013-03-08 06:29:02

+0

謝謝。我的語言不是英語 – user1279988 2013-03-08 06:41:16

回答

1

這是一個JavaScript三元運算符。見Details Here

var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX)/coef) : Math.round((this.topTileFromX - bounds.right)/coef); 

等價於下面的表達式

var x_num; 

if (this.topTileFromX < this.topTileToX) 
{ 
    x_num= Math.round((bounds.left - this.topTileFromX)/coef); 
} 
else 
{ 
    x_num= Math.round((this.topTileFromX - bounds.right)/coef); 
} 
+0

我明白了!謝謝。 – user1279988 2013-03-08 06:15:16

0

<意味着 「比較小」,如在數學。

因此

  • 2 < 3回報true
  • 2 < 2false
  • 3 < 2false
0
var x_num = this.topTileFromX < this.topTileToX ? Math.round((bounds.left - this.topTileFromX)/coef) : Math.round((this.topTileFromX - bounds.right)/coef); 

它的if語句更短。這意味着:

var x_num; 

if(this.topTileFromX < this.topTileToX) 
{ 
    x_num = Math.round((bounds.left - this.topTileFromX)/coef); 
} 
else 
{ 
    x_num = Math.round((this.topTileFromX - bounds.right)/coef); 
}