这就是我未压缩的样子:
function extractDropdownValues(){
var all_selected = true;
$('.variant_options select').each(function() {
if (this.selectedIndex == 0) {
all_selected = false;
}
});
这是压缩版本:
extractDropdownValues(){var e=!0;$(".variant_options select").each(function(){this.selectedIndex==0&&(e=!1)});
我对 e=!0
相当于 all_selected = true
以及 this.selectedIndex==0< 的压缩版本感到困惑
if 语句。在压缩版本中,它表示if
此索引== 0,则将其设置为 false。
但在压缩版本中,看起来它正在检查 selectedIndex
是否等于 0 AND &&
for all_selected = false
(即e=!1
)。但我不希望将其作为 if
条件,那应该是满足 if
条件后的结果。
我读错了吗?
附注压缩版本无法在生产中使用,但未压缩版本可以在开发中使用。我不确定这两者是否相关,但从表面上看,它们对我来说似乎并不相同。
编辑 1
正如我所怀疑的,我通过 JS prettifier 运行了缩小的 JS,结果如下:
function extractDropdownValues() {
var e = !0;
$(".variant_options select").each(function () {
this.selectedIndex == 0 && (e = !1)
});
苏...啊...这里发生了什么?
最佳答案
bool 形式的 0 为 false,因此 0(假)的 bool 否定为 true。
同样, bool 形式的 1 为 true,因此 1(true)的 bool 否定为 false。
&&
是短路“与”运算符,这意味着一旦它知道其结果是什么,它就会立即返回 - 如果其第一个操作数为 false,则返回 false。如果其第一个操作数为 true,则仅计算第二个操作数并返回该结果。
因此,仅当 this.selectedIndex==0
为 true 时,e=!1
才会执行(因为如果为 false,它知道它将返回 false 并提前中止) .
关于javascript - 这个压缩后的 JS 与未压缩版本有何相同之处?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15652221/