[英]Closure-compiler didn't flatten this if statement into a “guard” using &&
I don't understand why CC didn't codegolf this further. 我不明白为什么CC不能再对此进行编码。 I frequently write with "guard" statements like var x = obj.fun && obj.fun();
我经常用“ guard”语句编写,例如var x = obj.fun && obj.fun();
for example. 例如。 But CC doesn't reduce an if
into a "guard". 但是CC并没有将if
变成“守卫”。
Are the compiled js and expected js actually different? 编译后的js和预期的js是否真的不同?
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();
}
This code returns undefined
if the Math.random function does not exists: 如果Math.random函数不存在,则此代码返回undefined
:
return Math.random && 0.5 > Math.random();
I think that there may be a difference between returning false
and returning undefined
. 我认为返回false
和返回undefined
之间可能会有区别。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.