我只是在学习可选链和空值合并的组合。这里是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}`);
}};
当我试图检查该方法是否存在时,无论如何它都会打印出它们。我做错了什么?
console.log(restaurant.orderPizza?.('some', 'something') ?? 'no method exist');