Skip to content

Instantly share code, notes, and snippets.

@abinavseelan
Created May 2, 2018 15:40
Show Gist options
  • Save abinavseelan/369247d5c51b131a39ab66f10aadbf8a to your computer and use it in GitHub Desktop.
Save abinavseelan/369247d5c51b131a39ab66f10aadbf8a to your computer and use it in GitHub Desktop.
Class with chaining methods example
function Menu() {
this.items = [];
}
Menu.prototype.addItem = function(item) {
this.items.push(item);
return this; // This allows for chaining
}
Menu.prototype.showMenu = function() {
return this.items;
}
// Implementation
const menu = new Menu();
menu.addItem('Soup').addItem('Garlic Bread').addItem('Pizza');
menu.showMenu(); // ['Soup', 'Garlic Bread', 'Pizza']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment