Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IIFE #3

Open
martoio opened this issue Nov 25, 2019 · 2 comments
Open

IIFE #3

martoio opened this issue Nov 25, 2019 · 2 comments

Comments

@martoio
Copy link
Collaborator

martoio commented Nov 25, 2019

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

module.exports = (function(){
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'];
return {
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);
}
}
}
}());

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
Copy link
Collaborator Author

martoio commented Nov 25, 2019

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

@martoio
Copy link
Collaborator Author

martoio commented Nov 25, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@martoio and others