Typescript - Human Inheritance

TypeScript - JavaScript's Superset (Article) (Program)

32

Typescript - Restaurant Class Create a class "Restaurant" that has

⚫ one public member "menu" to store today's menu list,

⚫ a constructor to initialize it, and

⚫ function "list()" that display today's menu list.  In list() function, log the menu.

Sample Output: ['dosa', 'idly', 'chat']

Note: Input array is passed in the constructor of the class Restaurant

Program:

class Restaurant {
  public menu: string[];

  constructor(menu: string[]) {
    this.menu = menu;
  }

  list(): void {
    console.log(this.menu);
  }
}

// Example usage:
const todayMenu = ['dosa', 'idly', 'chat'];
const restaurant = new Restaurant(todayMenu);

// Calling the list method to display today's menu
restaurant.list();

Output:

// Example usage:
const todayMenu = ['dosa', 'idly', 'chat'];
const restaurant = new Restaurant(todayMenu);

// Calling the list method to display today's menu
restaurant.list();