请解释这行js代码。

时间:2013-03-08 06:13:10

标签: javascript

我有以下javascript代码块,并且不是很清楚:

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 <是什么意思?

3 个答案:

答案 0 :(得分: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);
}

答案 1 :(得分:0)

<的意思是“小于”,就像在数学中一样。

因此

  • 2 < 3返回true
  • 2 < 2false
  • 3 < 2也是false

答案 2 :(得分: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);
}