4 ms·
Objects not really safe to use as sets: const x = {}; ‘constructor’ in x // true x[‘constructor’] === Object // true You have to use hasOwnPropert
by zarzavat 2y ago
Objects not really safe to use as sets:
const x = {};
‘constructor’ in x // true
x[‘constructor’] === Object // true
You have to use hasOwnProperty and people don’t remember to do this.
Some properties cannot be deleted, and some cannot be modified:
delete x.constructor;
x.constructor === undefined // false
x.__proto__ = undefined;
x.__proto__ === undefined // false
Sets also allow you to have sets of other types rather than just strings and symbols.
- threatofrain 2y agoIf you do use objects I might suggest Object.create(null).
- tracker1 2y ago+1 Was going to be my response as well...
- kevincox 2y agoYup. I always use Set and Map over objects. They just contain way fewer surprises and the APIs actually work as you would expect. I only use Objects for "struct" like objects with a known set of keys that are used by name. It is also nice because it conveys more intent to the reader.