Front-End/javascript/chapter09/nullishCoalescingOperator.js

74 lines
1.8 KiB
JavaScript

const restauraunt = {
name: "Classico Italiano",
location: "Via Angelo Tavanti 23, Firenze, Italy",
categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
mainMenu: ["Pizza", "Pasta", "Risotto"],
order: function (starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
orderDelivery: function ({
starterIndex = 1,
mainIndex = 0,
time = "20:00",
address,
}) {
console.log(
`Order recieved! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0,
close: 24,
},
},
orderPasta: function (ing1, ing2, ing3) {
console.log(
`Here is your delicious pasta with ${ing1}, ${ing2} and ${ing3}`
);
},
orderPizza: function (mainIngridient, ...otherIngridients) {
console.log(mainIngridient);
console.log(otherIngridients);
},
};
restauraunt.numGuests = 0;
// nullisch Werte: null und undefined (nicht 0 und ""), wird nur dann evaluirt, nur wenn den Wert null oder undefined sein wird
const guests1 = restauraunt.numGuests ?? 10;
console.log(guests1);
const rest1 = {
name: "Capri",
numGuest: 20,
};
const rest2 = {
name: "La Piazza",
owner: "Giovanni Rossi",
};
rest1.numGuest = rest1.numGuest || 10;
rest2.numGuest = rest2.numGuest || 10;
// das Gleiche wie oben
rest2.numGuest ||= 10; // rest2.numGuest = rest2.numGuest || 10;
// verkürzte nullisch Abtreungsoperator
rest2.numGuest ??= 10; // rest2.numGuest = rest2.numGuest ?? 10;
console.log(rest1);
console.log(rest2);
rest2.owner &&= "<ANONYMOUS>";
console.log(rest2);