我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 0在一个假设声明。
能否取代 = = = = = = =?
任何性能改进都将被欢迎,因为有许多比较运营商。
如果没有类型转换发生,会有超越 ==的性能收益吗?
我使用JSLint通过JavaScript,它返回了许多建议,以取代 ==(两个平等的标志)与 ===(三个平等的标志)当做一些事情,如比较idSele_UNVEHtype.value.length == 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
其他回答
简单
= = = = = = = = = = = = = = = = = = = = = =
和
=== 是指在没有类型强迫的情况下进行操作的比较。
格式强制在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.
在这里的答案中,我没有读到什么是平等的意思,有些人会说 === 意味着平等和相同的类型,但这不是真的。
所以,让我们采取下列代码:
var a = [1,2,3];
var b = [1,2,3];
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
在这里相同:
var a = { x: 1, y: 2 };
var b = { x: 1, y: 2 };
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
var a = { };
var b = { };
var c = a;
var ab_eq = (a === b); // false (even though a and b are the same type)
var ac_eq = (a === c); // true
这种行为并不总是显而易见的,有更多的故事,而不是平等和同类。
规则是:
对于值类型(数字): a === b 如果 a 和 b 具有相同的值,并且具有相同的类型,则返回真实。
下一篇:特殊案例...
var a = "12" + "3";
var b = "123";
alert(a === b); // returns true, because strings behave like value types
但是,这个问题怎么样呢?
var a = new String("123");
var b = "123";
alert(a === b); // returns false !! (but they are equal and of the same type)
我以为线条像值类型一样行事吗? 好吧,这取决于你问谁...... 在这种情况下, a 和 b 不是相同的类型. a 是类型对象,而 b 是类型线条。
是的,平等 ==和身份 ===操作员之间有一个很大的差异. 通常,身份操作员表现更快,因为没有类型的转换完成. 但如果值是相同的类型,你会看到没有差异. 查看我的帖子 JavaScript 平等操作员的传说,解释了细节,包括类型的转换和比较算法,有很多例子。
javascript 是一个弱点编写的语言,即没有任何数据类型,如在 C、c++ 例如 int、boolean、 float 等,因此一个变量可以持有任何类型的值,这就是为什么这些特殊的比较操作员在那里。
艾格
var i = 20;var j = "20";
如果我们应用比较运营商,这些变量结果将是
i==j //result is true
或
j != i//result is false
为此,我们需要一个特殊的比较运营商,检查值以及变量的数据类型。
如果我们做
i===j //result is false
我的理性过程使用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 + "]")
};
}