我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。

能否取代 = = = = = = =?

任何性能改进都将被欢迎,因为有许多比较运营商。

如果没有类型转换发生,会有超越 ==的性能收益吗?


当前回答

让我们添加这个建议:

如果有疑问,请阅读规格!

請讓我解釋如何閱讀規格來澄清這個問題. 我看到,在這個非常古老的主題中,沒有人對非常奇怪的效果有答案. 所以,如果你能閱讀一個規格,這將非常幫助你在你的職業。

搜索 PDF 文件为 === 引导我到规格的第 56 页: 11.9.4. The Strict Equals Operator ( === ),然后通过规格我发现:

我发现新的线条(“a”)是一个对象,并使用步骤1:返回错误,如果类型不同。


Aidiakapi在下面的评论中写道:

从规格 11.2.2 新操作员:如果类型(构建器)不是对象,扔一个 TypeError 例外. 换句话说,如果 String 不是对象类型,它不能与新操作员一起使用。

而这最终意味着:新线(“a”)!==“a”。

其他回答

意思是平等 没有类型强制性 类型强制性 意思是 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 

在PHP和JavaScript中,它是一个严格的平等运营商,这意味着它将比较类型和值。

原因在上面的答案中被解释,而且杜格拉斯·克罗克福德也对此非常清楚(JavaScript:The Good Parts)。

但是,有一個例外: == null 是檢查“是零或不定義”的有效方式:

if( value == null ){
    // value is either null or undefined
}

例如,jQuery 1.9.1 使用此模式 43 次,而 JSHint 合成检查器甚至为此提供 eqnull 放松选项。

从 jQuery 风格指南:

严格的平等检查(===)应用于对 ==的支持,唯一的例外是通过零来检查非定义和零。

// Check for both undefined and null values, for some important reason. 
undefOrNull == null;

编辑 2021-03:

如今,大多数浏览器支持 Nullish coalescing 操作器(??) 和 Logical nullish 任务(??=),如果变量是 null 或 undefined,例如:

if (a.speed == null) {
  // Set default if null or undefined
  a.speed = 42;
}

可以用任何形式写作。

a.speed ??= 42;
a.speed ?? a.speed = 42;
a.speed = a.speed ?? 42;

我的理性过程使用eMAcs org-mode和node.js进行测试。

| use ==     | '' | '0' | false | 'false' | undefined | null | ' \t\r\n ' |
| ''         | x  | f   | t     | f       | f         | f    | f          |
| '0'        |    | x   | t     | f       | f         | f    | f          |
| false      |    |     | x     | f       | f         | f    | t          |
| 'false'    |    |     |       | x       | f         | f    | f          |
| undefined  |    |     |       |         | x         | t    | f          |
| null       |    |     |       |         |           | x    | f          |
| ' \t\r\n ' |    |     |       |         |           |      | x          | 



| use ===    | '' | '0' | false | 'false' | undefined | null | ' \t\r\n ' |
| ''         | x  | f   | f     | f       | f         | f    | f          |
| '0'        |    | x   | f     | f       | f         | f    | f          |
| false      |    |     | x     | f       | f         | f    | f          |
| 'false'    |    |     |       | x       | f         | f    | f          |
| undefined  |    |     |       |         | x         | f    | f          |
| null       |    |     |       |         |           | x    | f          |
| ' \t\r\n ' |    |     |       |         |           |      | x          |

下面的我的测试脚本:运行 > node xxx.js

var rowItems = ['', '0', false, 'false', undefined, null, ' \t\r\n ']
var colItems = rowItems

for(var i = 0; i < rowItems.length; i++) {
    for (var j = 0; j < colItems.length; j++) {
        var r = (rowItems[i] === colItems[j]) ? true : false;
        console.log(rowItems[i] + " = " + colItems[j] + " " + r + " [" + i + "] ==> [" + j + "]")
    };
}

如果您正在创建一个网页应用程序或安全的页面,您应该始终使用(只有在可能的情况下)

===

因为它会检查它是否是相同的内容,是否是相同的类型!

当有人进来时:

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 次 ==,这也意味着你有时会使用 == 而不是 === 因为它否则会阻止你的功能,但正如你说的那样,你应该使用 === 尽可能多)