多个运算符在一个变量声明中的重要性

时间:2012-05-31 14:24:29

标签: javascript

在测试页面是否已被缓存的this answer中,我看到了这个变量声明。

var isCached = currentCookie !== null;

=!==运算符在一个语句中的重要性是什么?

2 个答案:

答案 0 :(得分:2)

表达意味着:

isCached

currentCookie !== null为真,否则为

你应该像

一样阅读
var isCached = (currentCookie !== null)

或更详细地等同于

var isCached;
if (currentCookie !== null) {
   isCached = true;
}
else {
   isCached = false;
}

答案 1 :(得分:2)

该片段与以下内容相同:

var isCached = (currentCookie !== null);

换句话说,当且仅当isCached严格不等于空引用时,true才会设置为currentCookie