i am just learning about this combo of Optional chain and Nullish coalescing . Here is the object
const restaurant = {
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'],
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0, // Open 24 hours
close: 24,
},
},
orderPizza(ing1, ing2) {
console.log(`you have ordered your pizza with ${ing1} and ${ing2}`);
}};
as i am trying to check if the method exist,it is printing out both of them anyway.what am i doing wrong?
console.log(restaurant.orderPizza?.('some', 'something') ?? 'no method exist');
Perhaps return a value from the function otherwise it has an undefined value:
const restaurant1 = { name_: 'Classico Italiano', orderPizza(ing1, ing2) { return `you have ordered your pizza with ${ing1} and ${ing2}`; } }; const restaurant2 = { name_: 'Other', }; console.log(restaurant1.orderPizza?.('some', 'something')?? 'no method exist'); console.log(restaurant2.orderPizza?.('some', 'something')?? 'no method exist');
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.