我使用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.

其他回答

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

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

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

首先,一些关于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

操作员 = = = = = = = = = = = = = = = = = = = = = = = = = = =

此分類上一篇

缺乏过渡性是令人担忧的,我的建议是永远不要使用邪恶的双胞胎,相反,总是使用 === 和!== 所有仅仅显示的比较都是与 === 运营商虚假的。


更新:

var a = [1,2,3];
var b = [1,2,3];

var c = { x: 1, y: 2 };
var d = { x: 1, y: 2 };

var e = "text";
var f = "te" + "xt";

a == b            // false
a === b           // false

c == d            // false
c === d           // false

e == f            // true
e === f           // true

"abc" == new String("abc")    // true
"abc" === new String("abc")   // false

在这里 ==运营商正在检查两个对象的值,并返回真实,但 ===看到它们不是相同的类型,并返回虚假。 哪一个是正确的? 这真的取决于你正在试图比较什么。

参考 http://www.ecma-international.org/ecma-262/5.1/#sec-11.9.3

JavaScript 是虚拟的,就像 PHP 一样。

var x = "20";
var y =20;

if (x===y) // false

这将总是给你一个假的,因为尽管变量的值是相同的,数据类型是不一样的。

一个是,另一个是。

If(x==y)//true

但是,这只是检查内容是否相同,无论数据类型如何。

我不想说值是平等的,因为一个序列值不能与 int 值平等。

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

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

艾:

1 == "1" //true

1 === "1" //false

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