我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 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.

其他回答

它建议取代 ==与 ===的原因是 ===操作员比 ==更可靠。在我们的背景下可靠的手段 ===也去类型检查. 考虑到最好的编程做法,我们应该总是选择更可靠的功能比不可靠的一个。 再一次,每当我们想到的正是相同的操作员大部分时间,我们是默认考虑类型应该是相同的。

=== 运营商检查值以及变量的类型,以便平等。

操作员只检查平等变量的值。

因此,新 String(“Hello world”) ===“Hello world”返回虚假,但是, ==不关心如果对象是相同的,它只是将一个论点转换为另一个类型:如果转换是不可能的,返回虚假。

使用 === 如果你想在JavaScript中比较几件事,它被称为严格的平等,这意味着如果只有两种类型和值是相同的,就不会有任何不需要的类型纠正为你,如果你使用 ==,你基本上不关心类型,在许多情况下你可能会遇到问题与空虚的平等比较。

使用严格的平等 ===

var num = 0;
var obj = new String('0');
var str = '0';

console.log(num === num); // true
console.log(obj === obj); // true
console.log(str === str); // true

console.log(num === obj); // false
console.log(num === str); // false
console.log(obj === str); // false
console.log(null === undefined); // false
console.log(obj === null); // false
console.log(obj === undefined); // false

使用平等 = = =

空的平等比较两种平等值,然后将两种值转换为一个常见的类型。 转换后(一方或两方可能遭受转换),最终的平等比较按 === 进行。 空的平等是相同的: A == B 总是具有相同的序列为 B == A 的任何值 A 和 B (除了应用转换顺序)。

var num = 0;
var obj = new String('0');
var str = '0';

console.log(num == num); // true
console.log(obj == obj); // true
console.log(str == str); // true

console.log(num == obj); // true
console.log(num == str); // true
console.log(obj == str); // true
console.log(null == undefined); // true

// both false, except in rare cases
console.log(obj == null);
console.log(obj == undefined);

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