Retourner une stricte égalité
Bonjour,
Je tente de comprendre ce script :
Code:
1 2 3 4 5 6 7
|
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
} |
D'après ce que je comprends, la méthode isSelected doit retourner non seulement le produit courant mais il faut qu'il soit identique à l'attribut product.sku.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
import {
Component,
EventEmitter,
Input,
Output
} from '@angular/core';
import { Product } from '../product.model';
@Component({
selector: 'products-list',
templateUrl: './products-list.component.html'
})
export class ProductsListComponent {
/**
* @input productList - the Product[] passed to us
*/
@Input() productList: Product[];
/**
* @output onProductSelected - outputs the current
*
Product whenever a new Product is selected
*/
@Output() onProductSelected: EventEmitter<Product>;
/**
* @property currentProduct - local state containing
*
the currently selected `Product`
*/
private currentProduct: Product;
constructor() {
this.onProductSelected = new EventEmitter();
}
clicked(product: Product): void {
this.currentProduct = product;
this.onProductSelected.emit(product);
}
isSelected(product: Product): boolean {
if (!product || !this.currentProduct) {
return false;
}
return product.sku === this.currentProduct.sku;
}
} |
Que se passe-t-il si product.sku n'est pas strictement égale (==) ? Il retourne false ? Il retourne null ? Il ne retourne rien (void) ?
Dans ce cas, n'est-il pas plus simple d'écrire ceci :
Code:
1 2 3 4 5 6 7
|
isSelected(product: Product): boolean {
if (!product || !this.currentProduct || (product.sku != this.currentProduct.sku ) {
return false;
}
return this.currentProduct.sku;
} |
Merci de votre réponse.