我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
当前回答
如果您正在创建一个网页应用程序或安全的页面,您应该始终使用(只有在可能的情况下)
===
因为它会检查它是否是相同的内容,是否是相同的类型!
当有人进来时:
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中,它具有相同的值和类型。
例如,
4 == "4" // will return true
但
4 === "4" // will return false
因此,新 String(“Hello world”) ===“Hello world”返回虚假,但是, ==不关心如果对象是相同的,它只是将一个论点转换为另一个类型:如果转换是不可能的,返回虚假。
=== 检查相同的侧面在类型和值均等。
例子:
'1' === 1 // will return "false" because `string` is not a `number`
常见的例子:
0 == '' // will be "true", but it's very common to want this check to be "false"
另一个常见的例子:
null == undefined // returns "true", but in most cases a distinction is necessary
很多时候,一个不类型的检查会很有用,因为你不在乎值是否不定义,零,0或“。
简单
= = = = = = = = = = = = = = = = = = = = = =
和
=== 是指在没有类型强迫的情况下进行操作的比较。
格式强制在JavaScript中意味着自动将数据类型转换为其他数据类型。
例如:
123 == "123" // Returns true, because JS coerces string "123" to number 123
// and then goes on to compare `123 == 123`.
123 === "123" // Returns false, because JS does not coerce values of different types here.
JavaScript = = = = = = =
0==false // true
0===false // false, because they are of a different type
1=="1" // true, auto type coercion
1==="1" // false, because they are of a different type