Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 20th, 2012  |  syntax: None  |  size: 1.44 KB  |  views: 26  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. var cart = function(spec,my){
  2.  
  3.     // Private vars and functions
  4.  
  5.     var iNumItemsOrdered = 0;
  6.     var iNumItemsLeft= 2;
  7.  
  8.     my = my || {};
  9.  
  10.     init();
  11.  
  12.     function init(){
  13.         if (spec){
  14.             my.sItemLabel = spec
  15.         }else{
  16.             my.sItemLabel = "Item";
  17.         }
  18.  
  19.     }
  20.  
  21.     // Protected
  22.  
  23.     my.checkItemInventory = function(){
  24.         return (iNumItemsLeft > 0);
  25.     }
  26.  
  27.  
  28.     //Public api
  29.  
  30.     var that = {
  31.         createCart:function(){
  32.             console.log("CART. cart created.");
  33.         },
  34.  
  35.         placeOrder:function(){
  36.             console.log("CART. placed order for " + iNumItemsOrdered + " " + my.sItemLabel +  "s.");
  37.         },
  38.  
  39.         addItemToCart:function(){
  40.             if (my.checkItemInventory()){
  41.                 iNumItemsOrdered++;
  42.                 console.log("CART. " + my.sItemLabel +  " added.");
  43.                 iNumItemsLeft--;
  44.             }else{
  45.                 console.log("CART. sorry " + my.sItemLabel +  "s all gone!");
  46.             }
  47.         }
  48.     }
  49.  
  50.     return that;
  51.  
  52. };
  53.  
  54.  
  55.  
  56. var sooperCart = function(spec,my){
  57.  
  58.     my = my || {};
  59.  
  60.     // "Inherit" all methods from cart
  61.  
  62.     var that = cart(spec,my);
  63.  
  64.     // Add additional method
  65.  
  66.     that.addAllItems = function(){
  67.         console.log("CART. add ALL " + my.sItemLabel +  ".");
  68.  
  69.         while (my.checkItemInventory()){
  70.             that.addItemToCart();
  71.         }
  72.     }
  73.  
  74.     return that;
  75. };
clone this paste RAW Paste Data