我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
当前回答
操作员称为严格的比较操作员,与操作员不同。
将采取2 vars a 和 b。
对于“a = = b”来评估为真实的 a 和 b 必须是相同的值。
在“a === b”的情况下,a 和 b 必须是相同的值,也必须是相同的类型,以便它被评估为真实。
接下来的例子
var a = 1;
var b = "1";
if (a == b) //evaluates to true as a and b are both 1
{
alert("a == b");
}
if (a === b) //evaluates to false as a is not the same type as b
{
alert("a === b");
}
简而言之,使用 == 操作员可能会在您不希望使用 === 操作员这样做的情况下评估为真实。
在90%的使用场景中,不管你使用哪个,但当你有一天得到一些意想不到的行为时,知道差异是有用的。
其他回答
為什麼 ==是如此不可預測的?
如果你比较一个空的字符串“”与零的数字,你会得到什么?
它没有结束,这里还有一个:
'0' == false // true
[1] == true // true
[] == false // true
[[]] == false // true
[0] == false // true
随后,用绳子
[1,2,3] == '1,2,3' // true - REALLY?!
'\r\n\t' == 0 // true - Come on!
它变得更糟:
let A = '' // empty string
let B = 0 // zero
let C = '0' // zero string
A == B // true - ok...
B == C // true - so far so good...
A == C // **FALSE** - Plot twist!
让我们再说一遍:
(A == B) && (B == C) // true
(A == C) // **FALSE**
而这只是你与原始人得到的疯狂事物。
為什麼會發生這件事?
好吧,这是因为不同于“三重平等”(===),这只是检查两个值是否相同。
它有特殊的处理功能,特殊的处理零,无定义,线条,你命名它。
它变得相当可口可乐。
function isEqual(x, y) { // if `==` were a function
if(typeof y === typeof x) return y === x;
// treat null and undefined the same
var xIsNothing = (y === undefined) || (y === null);
var yIsNothing = (x === undefined) || (x === null);
if(xIsNothing || yIsNothing) return (xIsNothing && yIsNothing);
if(typeof y === "function" || typeof x === "function") {
// if either value is a string
// convert the function into a string and compare
if(typeof x === "string") {
return x === y.toString();
} else if(typeof y === "string") {
return x.toString() === y;
}
return false;
}
if(typeof x === "object") x = toPrimitive(x);
if(typeof y === "object") y = toPrimitive(y);
if(typeof y === typeof x) return y === x;
// convert x and y into numbers if they are not already use the "+" trick
if(typeof x !== "number") x = +x;
if(typeof y !== "number") y = +y;
// actually the real `==` is even more complicated than this, especially in ES6
return x === y;
}
function toPrimitive(obj) {
var value = obj.valueOf();
if(obj !== value) return value;
return obj.toString();
}
那么这意味着什么呢?
这意味着你可以用错误结束。
让你的生活更不复杂。
使用 === 而不是 ==。
操作员称为严格的比较操作员,与操作员不同。
将采取2 vars a 和 b。
对于“a = = b”来评估为真实的 a 和 b 必须是相同的值。
在“a === b”的情况下,a 和 b 必须是相同的值,也必须是相同的类型,以便它被评估为真实。
接下来的例子
var a = 1;
var b = "1";
if (a == b) //evaluates to true as a and b are both 1
{
alert("a == b");
}
if (a === b) //evaluates to false as a is not the same type as b
{
alert("a === b");
}
简而言之,使用 == 操作员可能会在您不希望使用 === 操作员这样做的情况下评估为真实。
在90%的使用场景中,不管你使用哪个,但当你有一天得到一些意想不到的行为时,知道差异是有用的。
让我们添加这个建议:
如果有疑问,请阅读规格!
請讓我解釋如何閱讀規格來澄清這個問題. 我看到,在這個非常古老的主題中,沒有人對非常奇怪的效果有答案. 所以,如果你能閱讀一個規格,這將非常幫助你在你的職業。
搜索 PDF 文件为 === 引导我到规格的第 56 页: 11.9.4. The Strict Equals Operator ( === ),然后通过规格我发现:
我发现新的线条(“a”)是一个对象,并使用步骤1:返回错误,如果类型不同。
Aidiakapi在下面的评论中写道:
从规格 11.2.2 新操作员:如果类型(构建器)不是对象,扔一个 TypeError 例外. 换句话说,如果 String 不是对象类型,它不能与新操作员一起使用。
而这最终意味着:新线(“a”)!==“a”。
如果您正在创建一个网页应用程序或安全的页面,您应该始终使用(只有在可能的情况下)
===
因为它会检查它是否是相同的内容,是否是相同的类型!
当有人进来时:
var check = 1;
if(check == '1') {
//someone continued with a string instead of number, most of the time useless for your webapp, most of the time entered by a user who does not now what he is doing (this will sometimes let your app crash), or even worse it is a hacker searching for weaknesses in your webapp!
}
但与
var check = 1;
if(check === 1) {
//some continued with a number (no string) for your script
} else {
alert('please enter a real number');
}
黑客将永远不会深入到系统中找到错误并攻击您的应用程序或用户
我的观点是,
===
将为您的脚本添加更多安全性
当然,你也可以检查输入的号码是否有效,是行,等等等,如果在第一例内的声明,但这至少对我来说更容易理解和使用。
我发布的原因是“更安全”或“安全”这个词从未在这个对话中说过(如果你看 iCloud.com 它使用 2019 次 === 和 1308 次 ==,这也意味着你有时会使用 == 而不是 === 因为它否则会阻止你的功能,但正如你说的那样,你应该使用 === 尽可能多)
在这里的答案中,我没有读到什么是平等的意思,有些人会说 === 意味着平等和相同的类型,但这不是真的。
所以,让我们采取下列代码:
var a = [1,2,3];
var b = [1,2,3];
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
在这里相同:
var a = { x: 1, y: 2 };
var b = { x: 1, y: 2 };
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
var a = { };
var b = { };
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
这种行为并不总是显而易见的,有更多的故事,而不是平等和同类。
规则是:
对于值类型(数字): a === b 如果 a 和 b 具有相同的值,并且具有相同的类型,则返回真实。
下一篇:特殊案例...
var a = "12" + "3";
var b = "123";
alert(a === b); // returns true, because strings behave like value types
但是,这个问题怎么样呢?
var a = new String("123");
var b = "123";
alert(a === b); // returns false !! (but they are equal and of the same type)
我以为线条像值类型一样行事吗? 好吧,这取决于你问谁...... 在这种情况下, a 和 b 不是相同的类型. a 是类型对象,而 b 是类型线条。