Reputation: 27247
I don't understand why CC didn't codegolf this further. I frequently write with "guard" statements like var x = obj.fun && obj.fun();
for example. But CC doesn't reduce an if
into a "guard".
Are the compiled js and expected js actually different?
Uncompiled source:
window.test = function () {
var ret = false;
if (Math.random) {
ret = Math.random() < 0.5;
}
return ret;
}
Command
npx google-closure-compiler \
--compilation_level ADVANCED \
--js test.js \
--js_output_file out.js
Compiled output (prettified):
window.test = function() {
var a = !1;
Math.random && (a = 0.5 > Math.random());
return a;
};
Expected output ("What I would have done"):
window.test = function () {
return Math.random && 0.5 > Math.random();
}
Upvotes: 0
Views: 38
Reputation: 17272
This code returns undefined
if the Math.random function does not exists:
return Math.random && 0.5 > Math.random();
I think that there may be a difference between returning false
and returning undefined
.
Upvotes: 1