2013-03-08 66 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); 
}