Advertisement
jordanov

Singleton

Nov 26th, 2020 (edited)
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // IMBD MOVIES
  2. // var popular =  newReleases.filter(a=>a.rating===5.0).map(a=>a.id);  get all movies with raiting >= 5.0
  3.  
  4. Singleton = function()
  5. {
  6.     let instance;
  7.     return {
  8.         getInstance: function ()
  9.         {
  10.             if (!instance)
  11.                 instance = Date.now();
  12.             return instance;
  13.         }
  14.     }
  15. }();
  16.  
  17. // ***************************************************
  18. // Shopping Cart functions
  19.  
  20. function shoppingCart(){
  21.     this.items = []
  22. }
  23.  
  24. shoppingCart.prototype.addItemToCart = function(name, price, count){
  25.     for(var i=0; i<this.items.length; i++)
  26.     {
  27.         if(this.items[i].name == name)
  28.         {
  29.             this.items[i].count += count;
  30.             return;
  31.         }
  32.     }
  33.  
  34.     this.items.push({
  35.         name: name,
  36.         price: price,
  37.         count: count
  38.     })
  39. }
  40. shoppingCart.prototype.setCountForItem = function(name, count){
  41.     if(count > 0)
  42.         for(var i=0; i<this.items.length;i++)
  43.         {
  44.             if(this.items[i].name == name)
  45.             {
  46.                 this.items[i].count = count;
  47.                 break;
  48.             }
  49.         }
  50.     else {
  51.         this.removeItemFromCart(name);
  52.     }
  53. }
  54.  
  55. shoppingCart.prototype.removeItemFromCartAll = function(name){
  56.     this.items = this.items.filter(el => {
  57.         return el.name != name
  58.     });
  59. }
  60.  
  61. shoppingCart.prototype.clearCart = function(){
  62.     this.items = []
  63. }
  64.  
  65. shoppingCart.prototype.countCart = function(){
  66.     return this.items.map(el => el.count).reduce((acc, el) => acc + el, 0);
  67. }
  68.  
  69. shoppingCart.prototype.totalCart = function() {
  70.     return this.items.reduce((acc, el) =>{
  71.         acc += el.price * el.count;
  72.         return acc;
  73.     }, 0);
  74. }
  75.  
  76. shoppingCart.prototype.listCart = function() {
  77.     return this.items.map(el => {
  78.         return {
  79.             ...el,
  80.             total: el.price * el.count
  81.         }
  82.     })
  83. }
  84.  
  85. var shoppingCart = new shoppingCart();
  86.  
  87.  
  88.  
  89. console.log("Testing Singleton!");
  90. console.log("Creating i1");
  91. console.log("i1 was created at time [Number: " + Singleton.getInstance() + "]");
  92. console.log("Creating i2 at time: " + Date.now());
  93. console.log("i2 was created with time [Number: " + Singleton.getInstance() + "]");
  94. let first =  Singleton.getInstance() === Singleton.getInstance();
  95. console.log("Checking if i1 and i2 are the same instance: " + first);
  96. console.log("Creating i3 in different execution context at time: " + Date.now());
  97. console.log("i3 was created with time [NUmber: " + Singleton.getInstance() + "]");
  98. let second = Singleton.getInstance() === Singleton.getInstance();
  99. let third = Singleton.getInstance() === Singleton.getInstance();
  100. let fourth = Singleton.getInstance() === Singleton.getInstance();
  101. console.log("Checking if i1 and i3 are the same variable: " + second);
  102. console.log("Checking if i2 and i3 are the same variable: " + third);
  103. console.log("Checking if i1 and i2 are the same variable: " + second);
  104.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement