Skip to the solution
MediumFrontend6 min read

What does Object.create do?

973 views

The problem

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 with proto set as its own [[Prototype]] β€” a real, direct way to set up prototypal inheritance without going through a constructor function or the class keyword 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 real TypeError (the method itself does not exist on it), and typeof obj.hasOwnProperty was 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 an Object.create(null) object genuinely did create a real, normal own property literally named __proto__ β€” confirmed directly via Object.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).

  • object-create

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 8 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

JavaScriptReal proof: Object.create(null) has genuinely no inherited methods, and makes __proto__ a plain own key

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 55 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How do you correctly check for NaN?

Continue