I am reading a javscript dojo library and I see many complex functions which I can't understand. For example:
_refreshUI: function () {
this._hasUI && (g.empty(this.flowContainer), f.forEach(this.basemaps, function (a, b) { a.id || (a.id = "basemap_" + b); this.flowContainer.appendChild(this._buildNodeLayout(a))
}, this), g.create("br", { style: { clear: "both" } }, this.flowContainer), this._markSelected(this._selectedBasemap))
this function is written on a single line. It contains functions separated with commas. So I can not read it.
I do not ask what the above function does.
What does this mean?:
this._hasUI && (firstFunction, secondFunction, ...)
What does it do? Or how can I write it clearly?
This is a way of only executing the functions if this._hasUI
resolves to true.
Try this:
true && (console.log(1), console.log(2));
And this:
false && (console.log(1), console.log(2));
You'll see that only the first line will run the console.log()
functions.
This is because the boolean AND operator (&&
) is evaluated lazily. If the left side of the operator resolves to false
, the interpreter won't bother evaluating the right side, because the operation can never result in true
. This means the functions on the right will only be executed if the left side is a truthy value.