我不明白为什么CC不这么做。例如,我经常使用像var x = obj.fun && obj.fun();这样的“卫士”语句编写代码。但CC并不会将if简化为“守卫”。
编译后的js和预期的js真的不同吗?
未编译的源代码:
window.test = function () {
var ret = false;
if (Math.random) {
ret = Math.random() < 0.5;
}
return ret;
}命令
npx google-closure-compiler \
--compilation_level ADVANCED \
--js test.js \
--js_output_file out.js编译输出(美化):
window.test = function() {
var a = !1;
Math.random && (a = 0.5 > Math.random());
return a;
};预期输出(“我会做什么”):
window.test = function () {
return Math.random && 0.5 > Math.random();
}发布于 2018-05-07 08:51:57
如果Math.random函数不存在,则此代码返回undefined:
return Math.random && 0.5 > Math.random();我认为返回false和返回undefined可能是有区别的。
https://stackoverflow.com/questions/50205803
复制相似问题