我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
当前回答
平等比较:
运营商 =
回归是真实的,当两个操作员是平等的时,操作员在比较之前转换为相同的类型。
>>> 1 == 1
true
>>> 1 == 2
false
>>> 1 == '1'
true
平等与类型比较:
主持人 ===
回来是真实的,如果两个操作员是相同的和类型。 一般情况下,如果你比较这个方式,它是更好的和更安全的,因为没有背后的场景类型转换。
>>> 1 === '1'
false
>>> 1 === 1
true
其他回答
这里是一个方便的比较表,显示发生的转换和 ==和 ===之间的差异。
如结论所说:
“使用三种平等,除非你完全理解两种平等发生的转变。
http://dorey.github.io/JavaScript - 平等 - 表/
意思是平等 没有类型强制性 类型强制性 意思是 JavaScript 不会自动将任何其他数据类型转换为序列数据类型
0==false // true,although they are different types
0===false // false,as they are different types
2=='2' //true,different types,one is string and another is integer but
javaScript convert 2 to string by using == operator
2==='2' //false because by using === operator ,javaScript do not convert
integer to string
2===2 //true because both have same value and same types
如果您正在创建一个网页应用程序或安全的页面,您应该始终使用(只有在可能的情况下)
===
因为它会检查它是否是相同的内容,是否是相同的类型!
当有人进来时:
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 次 ==,这也意味着你有时会使用 == 而不是 === 因为它否则会阻止你的功能,但正如你说的那样,你应该使用 === 尽可能多)
首先,一些关于JavaScript行方程式的术语:双方程式正式称为抽象平等比较运营商,而三方程式则称为严格平等比较运营商。
console.log(3 == "3"); // true
console.log(3 === "3"); // false.
console.log(3 == "3"); // true
console.log(3 === "3"); // false.
使用兩個平等的標誌返回真實,因為字符串“3”在比較之前轉換為3號。
console.log(true == '1'); // true
console.log(true === '1'); // false
console.log(true == '1'); // true
console.log(true === '1'); // false
再一次,抽象的平等比较进行类型转换,在这种情况下,两者都是真实的和“1”字符串转换为1号,结果是真实的。
如果你明白你正好在你的道路上区分 ==和 ===. 但是,有一些场景,其中这些运营商的行为是不直觉的。
console.log(undefined == null); // true
console.log(undefined === null); // false. Undefined and null are distinct types and are not interchangeable.
console.log(undefined == null); // true
console.log(undefined === null); // false. Undefined and null are distinct types and are not interchangeable.
console.log(true == 'true'); // false. A string will not be converted to a boolean and vice versa.
console.log(true === 'true'); // false
console.log(true == 'true'); // false. A string will not be converted to a boolean and vice versa.
console.log(true === 'true'); // false
下面的例子是有趣的,因为它描述了字符串与字符串对象不同。
console.log("This is a string." == new String("This is a string.")); // true
console.log("This is a string." === new String("This is a string.")); // false
console.log("This is a string." == new String("This is a string.")); // true
console.log("This is a string." === new String("This is a string.")); // false
JSLint 有时会给你一些不现实的理由来改变事物。 === 具有与 == 相同的性能,如果类型已经相同。
它只有当类型不相同时更快,在这种情况下它不会尝试转换类型,而是直接返回虚假。
因此,IMHO,JSLint可能用来写新的代码,但无用的过度优化应该以任何代价避免。
也就是说,在一个检查中没有理由改变 == 到 === 如果(一个 == 测试)当你知道它是因为一个可以只是一个线条。
修改大量的代码会浪费开发人员和评论家的时间,并无所得。