我使用的是 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
语句中的条件等于 bool 值 true
,则执行该代码。如果条件不返回 bool 值,则 javascript 使用类型强制将该条件解释为 bool 值。您可以在这里阅读有关类型强制的更多信息:
What exactly is Type Coercion in Javascript?
此处提供了一篇文章,给出了类型强制时不同类型的数据所采用的真/假值:
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.
关于javascript - Angular : module behavior, 如果 (""我不明白 JavaScript 代码),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34002697/