我刚穿过一块其用在地方赋值语句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