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

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

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

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


当前回答

首先,一些关于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可能用来写新的代码,但无用的过度优化应该以任何代价避免。

也就是说,在一个检查中没有理由改变 == 到 === 如果(一个 == 测试)当你知道它是因为一个可以只是一个线条。

修改大量的代码会浪费开发人员和评论家的时间,并无所得。

使用 = = 运营商(平等)

true == 1; //true, because 'true' is converted to 1 and then compared
"2" == 2;  //true, because "2" is converted to 2 and then compared

使用 === 运营商(身份)

true === 1; //false
"2" === 2;  //false

这是因为平等运营商 = = 输入强迫,这意味着翻译者在比较之前暗示地试图转换值。

另一方面,身份操作员 === 不做类型强制,因此在比较时不转换值。

操作员只是比较不数据类的值。

=== 运营商将值与其数据类型的比较进行比较。

艾:

1 == "1" //true

1 === "1" //false

此操作员(“===”)用于自动输入字符的语言,例如 PHP,JavaScript。

JavaScript 有严格的和类型转换的比较. 一个严格的比较(例如, ===)仅是真实的,如果操作器是相同的类型. 更常用的抽象比较(例如, ==)在进行比较之前将操作器转换为相同的类型。

平等(==)操作员将操作员转换为操作员,如果它们不属于相同类型,则适用严格的比较;如果操作员是数字或波兰,操作员将转换为数字,如果可能;否则如果操作员是序列,操作员将转换为数字,如果可能。

参考:比较运营商(Mozilla Developer Network)

原因在上面的答案中被解释,而且杜格拉斯·克罗克福德也对此非常清楚(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;