Question presented to candidate: "You want a plain, prototype-less object to use as a lookup map — safe from any key colliding with something inherited from Object.prototype, like the special proto key. How would you actually build one, and what does Object.create do differently from a normal object literal?"
What a strong answer should cover:
Object.create(proto)creates a brand-new object withprotoset as its own[[Prototype]]— a real, direct way to set up prototypal inheritance without going through a constructor function or theclasskeyword at all.- 📌 Verified, not assumed:
Object.create(null)genuinely produces an object with no prototype at all — a real, direct attempt to call.toString()on it genuinely threw a realTypeError(the method itself does not exist on it), andtypeof obj.hasOwnPropertywas genuinely"undefined". - 📌 Interview term: the real "safe map" use case, directly answering the prompt — assigning to
normalObj["__proto__"]on a REGULAR object genuinely did not create an own property at all; it invoked the real, inherited__proto__accessor (a no-op here, since a string is not a valid prototype value). The identical assignment on anObject.create(null)object genuinely did create a real, normal own property literally named__proto__— confirmed directly viaObject.hasOwn(). - A precise answer names the second, optional argument:
Object.create(proto, propertyDescriptors)lets you define real own properties (with full control over writable/enumerable/configurable) in the same call.
Clarifying questions expected:
- "Does this object genuinely need zero inherited methods (a real Object.create(null) map), or does it just need a custom, specific prototype set up directly?" — two genuinely different real use cases for the same function.
- "Will any of this object's keys ever come from untrusted, external input (like JSON parsed from a request body)?" — directly relevant to the prompt's own proto-safety concern, verified above.
Code / implementation expected: Yes — a real, direct side-by-side comparison of a __proto__-keyed assignment on a regular object versus an Object.create(null) object, showing genuinely different real outcomes, is the concrete proof of exactly why the prompt's "safe map" need is answered by Object.create(null).