我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
当前回答
在典型的脚本中不会有性能差异 更重要的是,一千个“==="比一千个“=="=====================================================
这个建议不是因为性能问题,而是因为类型强迫意味着(‘\t\r\n' == 0)是真实的。
其他回答
我的理性过程使用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 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
平等比较:
运营商 =
回归是真实的,当两个操作员是平等的时,操作员在比较之前转换为相同的类型。
>>> 1 == 1
true
>>> 1 == 2
false
>>> 1 == '1'
true
平等与类型比较:
主持人 ===
回来是真实的,如果两个操作员是相同的和类型。 一般情况下,如果你比较这个方式,它是更好的和更安全的,因为没有背后的场景类型转换。
>>> 1 === '1'
false
>>> 1 === 1
true
是的,平等 ==和身份 ===操作员之间有一个很大的差异. 通常,身份操作员表现更快,因为没有类型的转换完成. 但如果值是相同的类型,你会看到没有差异. 查看我的帖子 JavaScript 平等操作员的传说,解释了细节,包括类型的转换和比较算法,有很多例子。
首先,一些关于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