我从https://github.com/andrewgodwin
找到了一些代码 var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
'something' ? 'something' : 'something'
的含义是什么?
答案 0 :(得分:2)
它是javascript ref中的条件运算符。 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Conditional_Operator
答案 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