webwizard99 / salt-store-backend

Back end for salt store project, produced as part of Codecademy Premium program
0 stars 1 forks source link

IIFE #3

Open martoio opened 4 years ago

martoio commented 4 years ago

There's no actual benefit to having the IIFE, and it just makes your code harder to understand:

https://github.com/webwizard99/salt-store-backend/blob/dc880294543b5bbce10963657e84ac953de71e56/utilities/storeitems.js#L1-L43

This does the same thing:

  let items = [];

  const item = function(img, alt, name, price) {
    this.img = img;
    this.alt = alt;
    this.name = name;
    this.price = price;
  }

  const flavors = [ 'Mint', 'Peppermint', 'Frankincense', 'Lavender', 'Tea Tree', 'Lemon', 'Sage', 'Clove'];

 module.exports = {
    getItems: function(page, number) {
      let start = (page - 1) * number;
      let end = page * number;
      if (end >= items.length) {
        end = items.length -1;
      }
      return items.slice(start, end);
    },

    setItems: function(newItems) {
      items = newItems;
    },

    getItemsLength: function() {
      return items.length;
    },

    initTestItems: function(n) {
      // if (items != []) return;
      for (let i = 0; i < n; i++) {
        let flavor = Math.floor(Math.random() * flavors.length);
        let cost = (Math.random() * 2) + 4;
        let newItem = new item('./joanna-kosinska-Prfs32wh-o4-unsplash.jpg', 'A spoon-full of salt', `${flavors[flavor]} Salt`, `$${cost.toFixed(2)}`);
        items.push(newItem);
      }
    }
  }
martoio commented 4 years ago

For reference: https://stackoverflow.com/questions/32463512/is-there-any-reason-to-define-module-exports-using-an-iife

martoio commented 4 years ago

If you are keen on learning something cool, Node.JS modules are technically IIFEs behind the scenes: https://medium.com/better-programming/node-js-modules-basics-to-advanced-2464001229b6

So wrapping a module in an IIFE is the same as putting an IIFE in another IIFE - no real benefit