2016-05-24 4 views
0

Ich versuche, einen einzeiligen Code in Javascript zu verstehen, aber es ist nicht sehr offensichtlich.Konvertieren Sie eine JavaScript-Zeile in besser lesbaren Code

Die Linie, die funktioniert:

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) 

ich unten mit Code zu konvertieren versucht, aber es funktioniert nicht:

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); 
} 

Jede Idee?

Danke!


Nach Ihrer Hilfe der gute Code ist:

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); 
} 

Thank you!

+0

I 6 verschiedene JavaScript-Konzepte in den ersten drei Zeilen des erweiterten Code gezählt haben. Was genau verstehst du nicht darüber? – Quentin

+0

Das [Verschlüsselung] Tag auf dieser Frage bringt mich zum Lachen. –

+0

Also die verkleinerte Version funktioniert aber die erweiterte nicht? Ist das das Problem? (Bitte klären Sie die Frage) – SparK

Antwort

0

Der ternäre Bedingungsoperator hat die niedrigste Priorität.

Auch in (A || B), C B wird nur ausgewertet, wenn A ist falsch, aber C wird immer.

So würde der entsprechende Code sein:

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

Ich denke, Sie könnten die letzte Zeile 'this .__ Parent ...' zu einem erweitern, wenn auch – SparK

+0

Ja konnten wir, obwohl es viel häufiger als der Rest der Verkleinerung und noch lesbar ist, so dass ich es unberührt ließ. – floribon

+0

Erweitert, da es zwar häufiger ist, aber Neuankömmlinge finden es immer noch verwirrend, && zu verwenden, um den Auswertungsfluss zu steuern – SparK

Verwandte Themen