简体   繁体   English

* ng使用'==='进行对象比较

[英]*ngFor object comparison using '==='

Going through the Angular tutorial (Tour of Heroes), I've stumbled upon a comparison between two objects. 通过Angular教程(英雄之旅),我偶然发现了两个对象之间的比较。 Since this is not a common practice I've tried multiple comparisons inside the class and all returned false , except if the compared are hero[0] and selectedHero , the returned value is true , assuming we assigned selectedHero with hero[0] . 因为这不是一个常见的做法我已经试过多次比较里面的类和所有返回false ,但如果比较的是hero[0]selectedHero ,返回的值是true ,假设我们分配selectedHerohero[0]

Tour of Heores part of code where comparison occurs: Link to StackBlitz 浏览Heores部分代码,进行比较: 链接到StackBlitz

<ul class="heroes">
  <li *ngFor="let hero of heroes"
    [class.selected]="hero === selectedHero"
    (click)="onSelect(hero)">
    <span class="badge">{{hero.id}}</span> {{hero.name}}
  </li>
</ul>

Why is this comparison returning true? 为什么这个比较会恢复正常? What exactly is being compared here? 究竟是什么在这里进行比较? I haven't found any explanation yet. 我还没有找到任何解释。

=== is checking the value (if the subject is a number or a boolean), the type and the object reference. ===正在检查值(如果主题是数字或布尔值),类型和对象引用。 If you create 2 objects with identical attributes like the following, it will return false : 如果您创建2个具有相同属性的对象,如下所示,则返回false:

let a = { test: 'test' };
let b = { test: 'test' };
console.log(a === b); // gives false

because it's not the same reference. 因为它不是同一个参考。 So in your case, selectedHero === hero will be true if your 2 objects has the same reference. 因此,在您的情况下,如果您的2个对象具有相同的引用,则selectedHero === hero将为true。

EDIT : 编辑:

Another precision is that you can copy object reference in another : 另一个精度是您可以在另一个中复制对象引用:

let a = { test: 'test' };
let b = a;
console.log(a === b); // gives true

and then if you change attribute of one the reference, it will change for both : 然后,如果您更改一个引用的属性,它将同时更改:

let a = { test: 'test' };
let b = a;
a.test = 'foo';
console.log(b.test) // gives foo

Every time you click on li elemnt onSelect is executed and set the variable selectedHero with the elemnt hero from heroes array,heroes containe objects so the assaignment is just set selectedHero to be refrence to the current elemnt(hero) and when we try to comapre( == or ===) object the compare if the point to the same refrence or not 每当你点击li elemnt onSelect ,执行并使用来自英雄数组的elemnt英雄设置变量selectedHero ,英雄包含对象,所以只有设置selectedAero才能引用当前元素(英雄)并且当我们尝试comapre时( ==或===)对象比较,如果指向相同的参考

let a = {};
let b = {};
let c = a;

console.log(a === b); // false 
console.log(a === c); // true

=== on an Object checks the address of the object and not the value. === on对象检查对象的地址而不是值。 If you want more information you can look up how pointers work. 如果您想了解更多信息,可以查看指针的工作原理。

But basically if (hero === selectedHero) returns true then they point to the same memory address. 但基本上如果(hero === selectedHero)返回true,则它们指向相同的内存地址。 Therefore, if you change the value of selectedHero, it will also have change the value of hero[0]. 因此,如果更改selectedHero的值,它还将更改hero [0]的值。

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

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