我剛穿過一塊其用在地方賦值語句JavaScript代碼來的地方的邏輯&&
或邏輯OR
表達式將用於:的javascript:使用賦值語句,其中的條件表達式通常會
var geo;
function getGeoLocation() {
try {
if (!! navigator.geolocation) {
return navigator.geolocation;
} else {
return undefined;
}
} catch(e) {
return undefined;
}
}
if (geo = getGeoLocation()) {
// ^^^^^^^^^^^^^^^^^^^^^ this is the statement I am interested in
console.log('conditional expression was true/truthy');
}
我的問題是,從if
聲明的角度來看,當評估
geo = getGeoLocation()
時會返回什麼?
特別是它是什麼結果?這是什麼類型?
是
- 無論功能
getGeoLocation()
回來了?
(其中的類型將是truthy/falsy) - 它是作業的「結果」嗎?即是否分配了非空的東西?
(在這種情況下,「結果」可能是布爾值,真/假?) - 還是別的?
呀,用'地理位置返回()'會值被用來測試'if'條件。例如'if(a = false){console.log(「abc」)} undefined'但是if(a = true){console.log(「abc」)} VM402:1 abc' – tewathia
「result 「的分配是指定的值。也就是說賦值運算符返回賦值的值。這就是爲什麼'a = b = c =「test」'起作用的原因。 – nnnnnn
只需閱讀[賦值運算符的文檔](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Assignment_Operators#Assignment),其中明確指出「賦值操作計算結果爲分配的值「(換句話說,RHS)。 – 2016-08-13 04:37:49