DEV Community

MarcelGeo
MarcelGeo

Posted on

Bug of the day with module variable

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);
}

Top comments (0)