简体   繁体   English

Javascript ===(三等号)

[英]Javascript === (triple equals)

Give the following snippet给出以下片段

function countZeroes(array) {
  function counter(total, element) {
    return total + (element === 0 ? 1 : 0);
  }
  return reduce(counter, 0, array);
}
  1. What does the === do? === 有什么作用?
  2. Is reduce a built in function? reduce是内置函数吗? What does it do?它有什么作用?
  3. Please explain the steps of this program.请解释这个程序的步骤。

It is the strict equality operator.它是严格相等运算符。

It compares two values and checks to see if they are identical according to the Strict Equality Comparison Algorithm .它比较两个值并根据严格相等比较算法检查它们是否相同。

This is opposed to == , which will attempt to coerce one or both of the values being compared if they are of different types.这与==相反,如果它们的类型不同,它将尝试强制比较一个或两个值。 That one uses the Absract Equality Comparison Algorithm .那个使用抽象相等比较算法

The rules for the Abstract algorithm can be tricky. Abstract 算法的规则可能很棘手。 You're better off using === unless you have special need for == .除非您特别需要==否则最好使用===

From the MDC docs来自MDC 文档

The standard equality operators (== and !=) compare two operands without regard to their type.标准的相等运算符(== 和 !=)比较两个操作数而不考虑它们的类型。 The strict equality operators (=== and !==) perform equality comparisons on operands of the same type.严格相等运算符(=== 和 !==)对相同类型的操作数执行相等比较。 Use strict equality operators if the operands must be of a specific type as well as value or if the exact type of the operands is important.如果操作数必须是特定类型和值,或者操作数的确切类型很重要,请使用严格相等运算符。 Otherwise, use the standard equality operators, which allow you to compare the identity of two operands even if they are not of the same type.否则,请使用标准的相等运算符,它允许您比较两个操作数的身份,即使它们的类型不同。


With regard to the code, this part:关于代码,这部分:

(element === 0 ? 1 : 0)

...basically says if the value of element is exactly equal to 0 , then use 1 , otherwise use 0 . ...基本上是说如果element的值正好等于0 ,则使用1 ,否则使用0

So to take that entire line:所以要采取整条线路:

return total + (element === 0 ? 1 : 0);

...the return value of the function will be total + 1 if element equals 0 , otherwise the return value will be total + 0 . ...如果element等于0 ,函数的返回值将为total + 1 ,否则返回值将为total + 0

You could rewrite the code using an if-else statement:您可以使用if-else语句重写代码:

if( element === 0 ) {
    return total + 1;
} else {
    return total + 0;
}

=== is the same as == except it doesn't cast variables =====相同,只是它不强制转换变量

0 == '0' -> true
0 === '0' -> false

reduce isn't a built in function, but what it certainly does is run counter on each element of the array. reduce 不是内置函数,但它确实会在数组的每个元素上运行计数器。

so for each element of the array the element is checked to be 0 and if it is the total is incremented.所以对于数组的每个元素,元素被检查为0 ,如果是,则增加总数。

=== is the identity operator, it's like ==, but does not perform type conversion. === 是恒等运算符,与 == 类似,但不执行类型转换。

this function appears to count the number of zeros in an array and return the count.此函数似乎计算数组中零的数量并返回计数。

  • I assume that reduce() acts like Array.prototype.reduce我认为 reduce() 的作用类似于 Array.prototype.reduce

=== is strictly equal, both sides have to be of the same type and be equal. === 严格相等,两边必须是同类型且相等。 This is used to avoid the comparison of 2 unequal types (usually boolean false and a number 0)这用于避免比较 2 个不相等的类型(通常是布尔 false 和数字 0)

The "===" is means "exactly equals", as in the value is the same, as is the type. “===”的意思是“完全等于”,因为值是一样的,类型也是一样。 So ...所以 ...

var x = 5;
if (x === 5) {
    alert("This will happen");
} else {
    alert ("This won't");
}

It's rarely used.它很少使用。

The reduce function is likely the Array.prototype.reduce() method , which is used to apply a function to values in an array sequentially (sort-of). reduce 函数可能是Array.prototype.reduce() 方法,它用于将函数按顺序应用于数组中的值(排序)。 So, in this usage it is applying the 'counter' function to everything in the array, which will count the # of zeros in the array and return that.因此,在这种用法中,它将“计数器”函数应用于数组中的所有内容,这将计算数组中的零数并返回该值。

Its a very good question and generally developer coming from other languages always have difficulty understanding the importance of using === as compare to ==这是一个很好的问题,通常来自其他语言的开发人员总是难以理解使用 === 与 == 相比的重要性


   1. 5 == '5' //true    why? Because it do
   type conversion whereas in case with
   ===    5 === '5'//false because '5' is a string as compare to number 5.
   2. '\t\r\n' == 0 //true     this lack of transitivity is alarming and cause
   lot of errors. 
   3. Use JsLint ...it will help writing better JS code keep your code safe
   from this kind of issues.
   4. Moreover their is a performance penalty for using == when you are
   comparing number with a string.

In my test it turns out that there is little practical performance difference between == and ===.在我的测试中,== 和 === 之间的实际性能差异很小。 While the strict operator is marginally faster (roughly 10%) in most browsers when combined with explicit type conversion, such as a === +b, the only real performance gains will come from avoiding type conversion entirely.虽然在大多数浏览器中,严格运算符在与显式类型转换(例如 a === +b)结合使用时会稍微快一些(大约 10%),但唯一真正的性能提升将来自完全避免类型转换。 Converting a string to an integer for comparison with another integer is significantly slower (up to 10x) than simple comparing two integers.将字符串转换为整数以与另一个整数进行比较比简单地比较两个整数要慢得多(最多 10 倍)。 You should never allow integers to be stored as strings internally, as the type conversion will incur a performance penalty.你永远不应该允许整数在内部存储为字符串,因为类型转换会导致性能损失。

While that was the basic takeaway from the numbers, I did find one interesting outlier when testing with Firefox.虽然这是从数字中得出的基本结论,但在使用 Firefox 进行测试时,我确实发现了一个有趣的异常值。 In Firefox, the comparison a === +b is about 20x slower than the equivalent a == b when a is an integer and b is a string integer.在 Firefox 中,当 a 是整数且 b 是字符串整数时,比较 a === +b 比等效的 a == b 慢大约 20 倍。 This result seems suspicious to me, and nothing similar occurred in any other browser.这个结果对我来说似乎很可疑,在任何其他浏览器中都没有发生类似的情况。 Oddly, when the Firebug script debugger is turned on, this result changes, and a === +b becomes about 10% faster than the other.奇怪的是,当 Firebug 脚本调试器打开时,这个结果发生了变化,a === +b 变得比另一个快 10% 左右。 I'm not sure what to make of this result, but it does serve as a reminder that integers should always be stored in numbers, not in strings.我不确定如何处理这个结果,但它确实提醒我们,整数应该始终存储在数字中,而不是字符串中。

see other answer about ===.请参阅有关 === 的其他答案。
reduce it built in JS function which uses like "foreach", its moving on every elemnt in the array.减少它内置的 JS 函数,它像“foreach”一样使用,它在数组中的每个元素上移动。
it start with the inital value, which in your case its zero, and then call to counter() and on the first element.它从初始值开始,在您的情况下为零,然后调用 counter() 和第一个元素。
it check it, and return total(which is zero)+ 1 if the element is 0, after the returned value will be the "total" for the 2nd element in the array and so on....它检查它,如果元素为 0,则返回 total(为零)+ 1,在返回的值将是数组中第二个元素的“total”之后,依此类推......
in conclusion: the reduce call to counter on every element of the array,doing the test and adding its value to the (n-1)st element's returned value;结论:对数组的每个元素的减少调用 counter,进行测试并将其值添加到第 (n-1) 个元素的返回值;

=== is a strict equality comparison. ===是严格的相等比较。 The == operator in JavaScript does type coercion, which often has surprising results , like how ' ' == false . JavaScript ==运算符会强制类型转换,这通常会产生令人惊讶的结果,例如' ' == false So most JavaScript developers use === where possible.因此,大多数 JavaScript 开发人员都尽可能使用===

Hard to tell about reduce() .很难说reduce() That is not a built-in global function in JavaScript, but it likely refers to the reduce() method on JavaScript arrays .这不是 JavaScript 中的内置全局函数,但它可能指的是JavaScript 数组上reduce()方法 The reduce() method executes counter() once for every element in the array, and each time it calls counter() , it replaces total with the returned value from the counter() call. reduce()方法对数组中的每个元素执行counter()一次,每次调用counter() ,它都会用counter()调用的返回值替换total So the given function counts the number of elements that are strictly equal to zero in array .所以给定的函数计算array中严格等于零的元素数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM