var cart = function(spec,my){ // Private vars and functions var iNumItemsOrdered = 0; var iNumItemsLeft= 2; my = my || {}; init(); function init(){ if (spec){ my.sItemLabel = spec }else{ my.sItemLabel = "Item"; } } // Protected my.checkItemInventory = function(){ return (iNumItemsLeft > 0); } //Public api var that = { createCart:function(){ console.log("CART. cart created."); }, placeOrder:function(){ console.log("CART. placed order for " + iNumItemsOrdered + " " + my.sItemLabel + "s."); }, addItemToCart:function(){ if (my.checkItemInventory()){ iNumItemsOrdered++; console.log("CART. " + my.sItemLabel + " added."); iNumItemsLeft--; }else{ console.log("CART. sorry " + my.sItemLabel + "s all gone!"); } } } return that; }; var sooperCart = function(spec,my){ my = my || {}; // "Inherit" all methods from cart var that = cart(spec,my); // Add additional method that.addAllItems = function(){ console.log("CART. add ALL " + my.sItemLabel + "."); while (my.checkItemInventory()){ that.addItemToCart(); } } return that; };