简体   繁体   English

Dart 中的 == 和 === 有什么区别?

[英]What is the difference between == and === in Dart?

Does Dart support == and ===? Dart 支持 == 和 === 吗? What is the difference between equality and identity?平等和身份有什么区别?

Dart supports == for equality and identical(a, b) for identity. Dart 支持==表示相等, identical(a, b)表示相同。 Dart no longer supports the === syntax. Dart 不再支持===语法。

Use == for equality when you want to check if too objects are "equal".当你想检查对象是否“相等”时,使用==表示相等。 You can implement the == method in your class to define what equality means.您可以在您的类中实现==方法来定义相等的含义。 For example:例如:

class Person {
  String ssn;
  String name;

  Person(this.ssn, this.name);

  // Define that two persons are equal if their SSNs are equal
  bool operator ==(other) {
    return (other is Person && other.ssn == ssn);
  }
}

main() {
  var bob =  Person('111', 'Bob');
  var robert =  Person('111', 'Robert');

  print(bob == robert); // true

  print(identical(bob, robert)); // false, because these are two different instances
}

Note that the semantics of a == b are:注意a == b的语义是:

  • If either a or b are null , return identical(a, b)如果abnull ,则返回identical(a, b)
  • Otherwise, return a.==(b)否则,返回a.==(b)

Use identical(a, b) to check if two variables reference the same instance.使用identical(a, b)检查两个变量是否引用同一个实例。 identical is a top-level function found in dart:core . identicaldart:core中的顶级函数。

It should be noted that the use of the identical function in dart has some caveats as mentioned by this github issue comment :应该注意的是,在 dart 中使用identical的函数有一些注意事项,正如这个 github 问题评论所提到的:

The specification has been updated to treat identical between doubles like this:规范已更新为像这样对待双打之间的相同:

The identical() function is the predefined dart function that returns true iff its two arguments are either: identical() 函数是预定义的 dart 函数,如果它的两个参数是:

  • The same object.同一个对象。
  • Of type int and have the same numeric value.类型为 int 且具有相同的数值。
  • Of type double, are not NaNs, and have the same numeric value. double 类型,不是 NaN,并且具有相同的数值。

What this entails is that even though everything in dart is an object, and f and g are different objects, the following prints true .这意味着即使 dart 中的所有内容都是对象,并且fg是不同的对象,但以下内容会打印true

int f = 99;
int g = 99;
print(identical(f, g));

because ints are identical by their value, not reference.因为 int 的值相同,而不是引用。


So to answer your question, == is used to identify if two objects have the same value, but the identical is used to test for referential equality except in the case of double and int as identified by the excerpt above.因此,为了回答您的问题, ==用于标识两个对象是否具有相同的值,但identical用于测试引用相等性,除了上面摘录所标识的doubleint的情况。

See: equality-and-relational-operators请参阅: 相等和关系运算符

As DART is said to be related to javascript, where the === exists, I wish not be downvoted very quickly.由于据说 DART 与存在 === 的 javascript 相关,我希望不要很快被否决。

Identity as a concept means that 1 equals 1, but 1.0 doesn't equal 1, nor does false equal 0, nor does "2" equal 2, even though each one evaluates to each other and 1==1.0 returns true.身份作为一个概念意味着 1 等于 1,但 1.0 不等于 1,false 也不等于 0,“2”也不等于 2,即使它们相互求值并且 1==1.0 返回 true。

It should be noted that in Dart, identical works similarly to Javascript, where (5.0 == 5) is true , but identical(5.0, 5) is false .应该注意的是,在 Dart 中, identical的工作方式类似于 Javascript,其中(5.0 == 5)true ,但identical(5.0, 5)false

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

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