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

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

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

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


当前回答

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

其他回答

是啊!重要。

=== 操作员在 javascript 中检查值以及类型,在那里作为 == 操作员只检查值(需要时进行类型转换)。

此分類上一篇

您可以轻松地测试它. 在 HTML 文件中插入跟踪代码并在浏览器中打开它

<script>

function onPageLoad()
{
    var x = "5";
    var y = 5;
    alert(x === 5);
};

</script>

</head>

<body onload='onPageLoad();'>

现在修改 onPageLoad() 方法以警告(x == 5);你会得到真相。

操作员称为严格的比较操作员,与操作员不同。

将采取2 vars a 和 b。

对于“a = = b”来评估为真实的 a 和 b 必须是相同的值。

在“a === b”的情况下,a 和 b 必须是相同的值,也必须是相同的类型,以便它被评估为真实。

接下来的例子

var a = 1;
var b = "1";

if (a == b) //evaluates to true as a and b are both 1
{
    alert("a == b");
}

if (a === b) //evaluates to false as a is not the same type as b
{
    alert("a === b");
}

简而言之,使用 == 操作员可能会在您不希望使用 === 操作员这样做的情况下评估为真实。

在90%的使用场景中,不管你使用哪个,但当你有一天得到一些意想不到的行为时,知道差异是有用的。

=== 检查相同的侧面在类型和值均等。


例子:

'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行方程式的术语:双方程式正式称为抽象平等比较运营商,而三方程式则称为严格平等比较运营商。

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

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

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