Huh. I might actually start using sets over just simple objects. Never found a great reason to use them before these methods, other than to show off.
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. const s = new Set();
const a = {}, b = {};
s.add(a);
s.has(a); // true
s.has(b); // false
const o = {};
o[a] = true;
o[a]; // true
o[b]; // true, cause its cast to a string like "[object Object]"
I've also seen Map perform faster than using an object as a Map.It's O(1) for the latter (edit: I stand corrected, but probably close, as per spec).
Conversion to and from arrays even preserves order.
So I don't see these two use cases as "showing off", why should anybody do that?
The main "result" of the interview shouldn't just be "does the code work". The process is far, far more important for understanding how you write code.