什么'什么' ? '什么' :'东西'运营商意味着

时间:2018-02-10 07:17:37

标签: javascript jquery

我从https://github.com/andrewgodwin

找到了一些代码
    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";

'something' ? 'something' : 'something'的含义是什么?

3 个答案:

答案 0 :(得分:2)

答案 1 :(得分:2)

var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";

这意味着:

var ws_scheme;
if (window.location.protocol == "https:") {
    ws_scheme = "wss";
} else {
    ws_scheme = "ws";
}

答案 2 :(得分:2)

它被称为条件(三元)运算符。此运算符经常用作if语句的快捷方式。如果条件在“?”之前是真的然后是“?”之后的值被分配给变量,否则“:”之后的值被赋给变量。

详细信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator

相关问题