2016-05-24 12 views
0

我想了解Javascript中的單行代碼,但它不是很明顯。在更易讀的代碼中轉換javascript一行

this._iconNeedsUpdate = !0,this._expandBounds(t), t instanceof L.MarkerCluster ? (e || (this._childClusters.push(t), t.__parent = this), this._childCount += t._childCount) : (e || this._markers.push(t), this._childCount++), this.__parent && this.__parent._addChild(t, !0) 

我試着用下面的代碼轉換,但它不工作:

this._iconNeedsUpdate = !0; 
this._expandBounds(t); 
if (t instanceof L.MarkerCluster) { 
    if (!e) { 
     this._childClusters.push(t); 
     t.__parent = this; 
    } else { 
     this._childCount += t._childCount; 
    } 
} else { 
    if (!e) { 
     this._markers.push(t); 
     this._childCount++; 
    } 
} 
if (this.__parent) { 
    this.__parent._addChild(t, !0); 
} 

任何想法

其中工程線路?

謝謝!


你的幫助後,良好的代碼是:

this._iconNeedsUpdate = true; 
this._expandBounds(t); 

if (t instanceof L.MarkerCluster) { 
    if (!e) { 
    this._childClusters.push(t); 
    t.__parent = this; 
    } 
    this._childCount += t._childCount 
} else { 
    if (!e) { 
    this._markers.push(t); 
    } 
    this._childCount++; 
} 
if (this.__parent) { 
    this.__parent._addChild(t, true); 
} 

謝謝!

+0

我在前三行代碼展開的計算6個不同的JavaScript概念。究竟是什麼,你不明白嗎? – Quentin

+0

這個問題上的[加密]標籤讓我發笑。 –

+0

所以縮小版本可以工作,但擴展版本不能?這是問題嗎? (請說明問題) – SparK

回答

0

三元條件運算符的優先級最低。

此外,在(A || B), C B只會被評估如果A是虛假的,但C總是會。

所以等效代碼爲:

this._iconNeedsUpdate = true; 
this._expandBounds(t); 

if (t instanceof L.MarkerCluster) { 
    if (!e) { 
    this._childClusters.push(t); 
    t.__parent = this; 
    } 
    this._childCount += t._childCount 
} else { 
    if (!e) { 
    this._markers.push(t); 
    } 
    this._childCount++; 
} 
if (this.__parent) { 
    this.__parent._addChild(t, true); 
} 
+0

我想你可以將最後一行'this .__ parent ...'擴展爲if if太 – SparK

+0

是的,儘管它比其他縮小更常見,並且仍然可讀,所以我沒有修改它。 – floribon

+0

擴展,因爲即使它更常見,新手仍然會覺得使用&&來控制評估流程很困惑 – SparK