Object.create Polyfill

ObjectsPrototypesPolyfill

Implement Object.myCreate(proto, propertiesObject).

Requirements:

  • proto must be an object, function, or null — throw TypeError otherwise
  • Return a new object whose [[Prototype]] is proto
  • If propertiesObject is provided, apply it via Object.defineProperties
  • Use the empty-constructor trick (the expected interview answer)

Examples

Example 1
Input
const animal = { speak() { return 'roar'; } };
const lion = Object.myCreate(animal);
console.log(lion.speak());
console.log(Object.getPrototypeOf(lion) === animal);
Output
roar
true
Explanation
The new object inherits speak() through the prototype chain.

Notes

  • The empty-constructor trick: `function F() {}; F.prototype = proto; return new F()`
  • Cannot produce true null-prototype objects — mention this limitation in an interview

Hints

Read the full write-up for Object.create Polyfill
</>JavaScript
Loading editor…
Test Result
Run your code, or Submit to test it