Out of curiousity, what is the expected behaviour if we:
items.add('apple', 2, false);
items.add('apple', 3);
What do you intend to happen in that case?
Also, you could put scannable_items inside the constructor of your Item class, as a variable. As it is only ever accessed via the new Item()
, or the items
functionality, it might make sense for it not to exist until the Item has been constructed.
let scannable_items = new Object();
You very rarely need to use the new Object()
constructor. Assigning a variable to new Object()
is just the same as assigning it to an object literal ({}
).
More importantly, why is this a global? Making it global means that if you have two instances of Item
, both share the same list:
const a = new Item(); a.add('apple', 1); a.add('banana', 2); const b = new Item(); b.add('pear', 3); a.list(); // ["apple", "banana", "pear"] a.all() === b.all(); // true
For that matter, why is Item
called Item
? It seems more like an ItemList
.
module.exports.items = new Item();
If it’s useful having this be a class
at all, you should export the class itself, rather than an instance of it, i.e. module.exports = { Item };
. Allow the importing module to do the instantiation.