Javascript Javascript逻辑“!==”运算符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10871630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript logical "!==" operator?
提问by Cory Gross
I am getting back into web development, and have been trying to go over the nuances of jscript recently. I was pouring through the source of the THREEx extension library built on top of Three.JS and noticed this function
我正在重新开始 Web 开发,并且最近一直在尝试了解 jscript 的细微差别。我正在浏览构建在 Three.JS 之上的 THREEx 扩展库的源代码,并注意到了这个功能
THREEx.KeyboardState.prototype.pressed = function(keyDesc)
{
var keys = keyDesc.split("+");
for(var i = 0; i < keys.length; i++){
var key = keys[i];
var pressed;
if( THREEx.KeyboardState.MODIFIERS.indexOf( key ) !== -1 ){
pressed = this.modifiers[key];
}else if( Object.keys(THREEx.KeyboardState.ALIAS).indexOf( key ) != -1 ){
pressed = this.keyCodes[ THREEx.KeyboardState.ALIAS[key] ];
}else {
pressed = this.keyCodes[key.toUpperCase().charCodeAt(0)];
}
if( !pressed) return false;
};
return true;
}
I am looking in particular at the line here:
我特别关注这里的行:
if( THREEx.KeyboardState.MODIFIERS.indexOf( key ) !== -1 ){
I am not familiar with this !== operator. I checked w3schools and their logical operators list does not have this one included. I am not sure if this is misspelled and the browsers simply count it as != or if it has some other meaning. Also I was wondering whether this is actually a single logical operator or whether it is some kind of combination, like ! + ==?
我不熟悉这个 !== 运算符。我检查了 w3schools,他们的逻辑运算符列表没有包含这个。我不确定这是否拼写错误,浏览器只是将其视为 != 或者它是否具有其他含义。另外我想知道这实际上是一个逻辑运算符还是某种组合,例如 ! + ==?
回答by Wormbo
You can find ===
and !==
operators in several other dynamically-typed languages as well. It always means that the two values are not only compared by their "implied" value (i.e. either or both values might get converted to make them comparable), but also by their original type.
您也可以在其他几种动态类型语言中查找===
和!==
操作符。这总是意味着这两个值不仅通过它们的“隐含”值进行比较(即可能会转换一个或两个值以使其具有可比性),而且还通过它们的原始类型进行比较。
That basically means that if 0 == "0"
returns true, 0 === "0"
will return false because you are comparing a number and a string. Similarly, while 0 != "0"
returns false, 0 !== "0"
returns true.
这基本上意味着如果0 == "0"
返回 true,0 === "0"
将返回 false,因为您正在比较一个数字和一个字符串。同样,while0 != "0"
返回 false,0 !== "0"
返回 true。
回答by Joe White
It's !=
without type coercion. See the MDN documentation for comparison operators.
Also see this StackOverflow answer, which includes a quote from "JavaScript: The Good Parts" about the problems with ==
and !=
. (null == undefined
, false == "0"
, etc.)
也看到这个StackOverflow的答案,其中包括来自“JavaScript的:好零件”一帖有关使用问题==
和!=
。(null == undefined
,false == "0"
等)
Short answer: alwaysuse ===
and !==
unless you have a compelling reason to do otherwise. (Tools like JSLint, JSHint, ESLint, etc. will give you this same advice.)
简短的回答:除非您有令人信服的理由不这样做,否则请始终使用===
and !==
。(如工具的JSLint,JSHint,ESLint,等会给你这个同样的建议。)
回答by kay - SE is evil
Copied from the formal specification: ECMAScript 5.1section 11.9.5
复制自正式规范:ECMAScript 5.1section 11.9.5
11.9.4 The Strict Equals Operator ( === )
The production EqualityExpression: EqualityExpression=== RelationalExpressionis evaluated as follows:
- Let lrefbe the result of evaluating EqualityExpression.
- Let lvalbe GetValue(lref).
- Let rrefbe the result of evaluating RelationalExpression.
- Let rvalbe GetValue(rref).
- Return the result of performing the strict equality comparison rval=== lval. (See 11.9.6)
11.9.5 The Strict Does-not-equal Operator ( !== )
The production EqualityExpression : EqualityExpression!== RelationalExpressionis evaluated as follows:
- Let lrefbe the result of evaluating EqualityExpression.
- Let lvalbe GetValue(lref).
- Let rrefbe the result of evaluating RelationalExpression.
- Let rvalbe GetValue(rref). Let rbe the result of performing strict equality comparison rval=== lval. (See 11.9.6)
- If ris true, return false. Otherwise, return true.
11.9.6 The Strict Equality Comparison Algorithm
The comparison x=== y, where xand yare values, produces trueor false. Such a comparison is performed as follows:
- If Type(x) is different from Type(y), return false.
- Type(x) is Undefined, return true.
- Type(x) is Null, return true.
- Type(x) is Number, then
- If xis NaN, return false.
- If yis NaN, return false.
- If xis the same Number value as y, return true.
- If xis +0 and y is -0, return true.
- If xis -0 and y is +0, return true.
- Return false.
- If Type(x) is String, then return trueif xand yare exactly the same sequence of characters (same length and same characters in corresponding positions); otherwise, return false.
- If Type(x) is Boolean, return trueif xand y are both trueor both false; otherwise, return false.
- Return trueif xand yrefer to the same object. Otherwise, return false.
11.9.4 严格等于运算符 ( === )
产生式EqualityExpression: EqualityExpression=== RelationalExpression的计算方式如下:
- 让lref是评估EqualityExpression的结果。
- 令lval为 GetValue( lref)。
- 让rref是评估RelationalExpression的结果。
- 令rval为 GetValue( rref)。
- 返回执行严格相等比较的结果rval=== lval。(见 11.9.6)
11.9.5 严格不等于运算符 ( !== )
产生式 EqualityExpression : EqualityExpression!== RelationalExpression的计算方式如下:
- 让lref是评估EqualityExpression的结果。
- 令lval为 GetValue( lref)。
- 让rref是评估RelationalExpression的结果。
- 令rval为 GetValue( rref)。令r为执行严格相等比较的结果rval=== lval。(见 11.9.6)
- 如果r为true,则返回false。否则,返回true。
11.9.6 严格相等比较算法
比较x=== y,其中x和y是值,产生true或false。这样的比较如下:
- 如果 Type( x) 与 Type( y) 不同,则返回false。
- Type( x) 未定义,返回true。
- Type( x) 为 Null,返回true。
- Type( x) 是数字,那么
- 如果x是 NaN,则返回false。
- 如果y是 NaN,则返回false。
- 如果x与y 的Number 值相同,则返回true。
- 如果x为 +0 且 y 为 -0,则返回true。
- 如果x为 -0 且 y 为 +0,则返回true。
- 返回false。
- 如果 Type( x) 是 String,那么如果x和y是完全相同的字符序列(相同的长度和相应位置的相同字符),则返回true;否则,返回false。
- 如果 Type( x) 是布尔值,如果x和 y 都为真或都为假,则返回真;否则,返回false。
- 如果x和y引用同一个对象,则返回true。否则,返回false。
回答by Brendan Cutajar
回答by secretformula
The !==
opererator tests whether values are not equal or not the same type.
i.e.
所述!==
opererator测试是否值不等于或不相同的类型。IE
var x = 5;
var y = '5';
var 1 = y !== x; // true
var 2 = y != x; // false
回答by Rahul
!==
!==
This is the strict not equal operator and only returns a value of true if both the operands are not equal and/or not of the same type. The following examples return a Boolean true:
这是严格的不等于运算符,仅当两个操作数不相等和/或类型不同时才返回 true 值。以下示例返回布尔值 true:
a !== b
a !== "2"
4 !== '4'