Javascript ===(三等于) [英] Javascript === (triple equals)

查看:120
本文介绍了Javascript ===(三等于)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提供以下代码段

function countZeroes(array) {
  function counter(total, element) {
    return total + (element === 0 ? 1 : 0);
  }
  return reduce(counter, 0, array);
}




  1. ===做什么?

  2. reduce 内置函数?它有什么作用?

  3. 请解释此计划的步骤。

  1. What does the === do?
  2. Is reduce a built in function? 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. You're better off using === unless you have special need for ==.

来自 MDC docs

From the MDC docs


标准相等运算符(==和!=)比较两个操作数而不考虑它们的类型。严格相等运算符(===和!==)对相同类型的操作数执行相等比较。如果操作数必须是特定类型以及值,或者操作数的确切类型很重要,请使用严格相等运算符。否则,使用标准相等运算符,它允许您比较两个操作数的标识,即使它们的类型不同。

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)

...基本上说如果的值元素 完全等于 0 ,然后使用 1 ,否则使用 0

...basically says if the value of element is exactly equal to 0, then use 1, otherwise use 0.

所以要占用整行:

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

...函数的返回值为 total + 1 如果元素等于 0 ,否则返回值将总计+ 0

...the return value of the function will be total + 1 if element equals 0, otherwise the return value will be total + 0.

您可以使用 if-else 语句重写代码:

You could rewrite the code using an if-else statement:

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

这篇关于Javascript ===(三等于)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆