DEV Community

loading...

Bug of the day with module variable

marcelgeo profile image MarcelGeo ・1 min read

Let´s see code bellow:

// Item.js
let active = null;

const setActive = item => {
  active = item;
}

export const activateItem = item => {
  active = setActive(item);
}

Where´s the problem? After several hours of debugging of our js code for activating modules. I found out this bug in similar code as above.
Variable active is global for module. If you will call function activateItem from outside of module, the value of active is undefined. Why? Method setAcive returns nothing:


NOTHING = undefined; // :D

Let's go to repair this situation:

// Item.js
let active = null;

const setActive = item => {
  active = item;
}

export const activateItem = item => {
  setActive(item);
}

Discussion (0)

pic
Editor guide