我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
当前回答
意思是平等 没有类型强制性 类型强制性 意思是 JavaScript 不会自动将任何其他数据类型转换为序列数据类型
0==false // true,although they are different types
0===false // false,as they are different types
2=='2' //true,different types,one is string and another is integer but
javaScript convert 2 to string by using == operator
2==='2' //false because by using === operator ,javaScript do not convert
integer to string
2===2 //true because both have same value and same types
其他回答
我的理性过程使用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 + "]")
};
}
使用 = = 运营商(平等)
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
这是因为平等运营商 = = 输入强迫,这意味着翻译者在比较之前暗示地试图转换值。
另一方面,身份操作员 === 不做类型强制,因此在比较时不转换值。
因此,新 String(“Hello world”) ===“Hello world”返回虚假,但是, ==不关心如果对象是相同的,它只是将一个论点转换为另一个类型:如果转换是不可能的,返回虚假。
JSLint 有时会给你一些不现实的理由来改变事物。 === 具有与 == 相同的性能,如果类型已经相同。
它只有当类型不相同时更快,在这种情况下它不会尝试转换类型,而是直接返回虚假。
因此,IMHO,JSLint可能用来写新的代码,但无用的过度优化应该以任何代价避免。
也就是说,在一个检查中没有理由改变 == 到 === 如果(一个 == 测试)当你知道它是因为一个可以只是一个线条。
修改大量的代码会浪费开发人员和评论家的时间,并无所得。
意思是平等 没有类型强制性 类型强制性 意思是 JavaScript 不会自动将任何其他数据类型转换为序列数据类型
0==false // true,although they are different types
0===false // false,as they are different types
2=='2' //true,different types,one is string and another is integer but
javaScript convert 2 to string by using == operator
2==='2' //false because by using === operator ,javaScript do not convert
integer to string
2===2 //true because both have same value and same types