Created
May 2, 2018 15:40
-
-
Save abinavseelan/369247d5c51b131a39ab66f10aadbf8a to your computer and use it in GitHub Desktop.
Class with chaining methods example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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