Angular: 模块行为,我不明白 javascript 代码 if ("")

Angular: module behavior, I don't understant javascript code if ("")

我正在使用 angularjs 1.4.3。而且我有一个好奇心,因为我看不懂Jasmine Spec Runner生成的代码的一段代码。

当我们生成它时,Jasmine(通过 ChutzPath)创建此代码:

    (function () {
        var amdTestPaths = [];
        if (window.require && typeof window.require === "function" && amdTestPaths.length > 0) {
            if (window.chutzpah) {
                window.chutzpah.usingModuleLoader = true;
            }

            if("") {
                require.config({
                    baseUrl: ""
                });
            }

            require.config({
                map: {'*': { } }
            });

            window.require(amdTestPaths, function () {    
                console.log("!!_!! Stating Jasmine from AMD callback...");            
                window.initializeJasmine();
            });
        } else {
           var currentWindowOnload = window.onload;

           window.onload = function() {
           if (currentWindowOnload) {
               currentWindowOnload();
           }

           window.initializeJasmine();
       };
   }
})();

if("") 是什么?我知道这是个愚蠢的问题,但我不明白

也许就像 if (true) 或 if (1)?

如您所知,if 语句中的代码在其条件评估为布尔值 true 时执行。如果条件不是 return 布尔值,javascript 使用类型强制将条件解释为布尔值。您可以在此处阅读有关类型强制的更多信息:

What exactly is Type Coercion in Javascript?

一篇文章给出了 true/false 不同类型的数据在强制类型可用时所采用的值:

https://javascriptweblog.wordpress.com/2011/02/07/truth-equality-and-javascript/

引用您案例中的相关部分:

Conditionals

In JavaScript, all conditional statements and operators follow the same coercion paradigm. We’ll use the if statement by way of example.

The construct if ( Expression ) Statement will coerce the result of evaluating the Expression to a boolean using the abstract method ToBoolean for which the ES5 spec defines the following algorithm:

Argument Type   Result
Undefined       false
Null            false
Boolean         The result equals the input argument (no conversion).
Number          The result is false if the argument is +0, −0, or NaN;
                otherwise the result is true.
String          The result is false if the argument is the empty String 
                (its length is zero); otherwise the result is true.
Object          true.