gpt4 book ai didi

JavaScript Uglify 不同并且正确吗?

转载 作者:行者123 更新时间:2023-12-03 00:44:00 26 4
gpt4 key购买 nike

我有一个丑化的JS文件,我想在其中获取美化版本,然后再次用grunt对其进行丑化。我希望它与第一个丑化文件相同。

但是当我这样做时,带有“if”的部分是不同的。

这是丑化文件的一部分:

...function sendRequest(requestParams){if(Object.keys(requestParams).length>=1){$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}}function...

但是美化文件中的丑化版本如下所示:

...function sendRequest(requestParams){Object.keys(requestParams).length>=1&&$.ajax({type:"POST",url:VideoSearchUrl,data:requestParams,dataType:"xml"}).done(function(xmlDoc){buildResult($(xmlDoc)),responses++,requests==responses&&postSearchActions()})}function...

为什么去掉“if”?在在线丑化器中它仍然存在。

或者意思是一样的吗?

最佳答案

这是正确的,因为 Short Circuit Evaluation .

As logical expressions are evaluated left to right, they are tested for possible "short-circuit" evaluation using the following rules:

  • false && (anything) is short-circuit evaluated to false.
  • true || (anything) is short-circuit evaluated to true.

The rules of logic guarantee that these evaluations are always correct. Note that the anything part of the above expressions is not evaluated, so any side effects of doing so do not take effect. Also, note that the anything part of the above expression is any single logical expression (as indicated by the parentheses).

(我强调)

a = 1;
b = 1;

if (a == b) {
console.log(1);
}
// will log because it's like
// (true) && expression
a == b && console.log(2);

// will NOT log because it's like
// (false) && expression
a != b && console.log(3);

何苦呢?

true&&expression 短于
if(true){表达式}(每个字节都很重要)

关于JavaScript Uglify 不同并且正确吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53322290/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com