我正在查看某人的最小化 JavaScript 代码,发现语法没有任何意义。
firstObject.init = function() {
void 0 === secondObject.properties && thirsObject.reportError("Something is wrong");
firstObject.doSomething();
}
我的猜测是,打包程序正在检查未定义的属性,在这种情况下会脱离执行上下文并返回 null。
if (secondObject.properties === undefined) {
thirdObject.reportError("Something is wrong");
return NULL;
}
这是怎么回事?
最佳答案
如果您查看 void 0
的结果,很快就会清楚发生了什么:
> void 0
undefined
因此,写作
void 0 === secondObject.properties
只是一种不同的书写方式
typeof secondObject.properties === "undefined"
Does this end the execution of the function or simply continues after executing the code to the right of the && operator?
条件评估通常是惰性“短路”,也就是说,如果已经确定某个条件将评估为真或假,则不再评估进一步的子条件。例如:
false && alert("foo"); // Will never alert foo
true && alert("foo"); // Will always alert foo
如果您有任何两个由 &&
运算符组合的条件,如果第一个(左侧)条件为 false,则无论其值是什么,整个条件都将为 false。因此,第二个条件的评估是不必要的,因此被丢弃。
更多信息:
- <删除> Lazy evaluation在维基百科上
- Short-circuit evaluation
关于javascript - javascript 加壳器如何使用 "void 0"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14603108/