Object.create Polyfill
Implement myObjectCreate(proto) — a polyfill for Object.create().
Object.create(proto) creates a new empty object whose [[Prototype]] is set to proto.
const animal = { eat() { return `${this.name} eats`; } };
const dog = myObjectCreate(animal);
dog.name = "Rex";
dog.eat(); // "Rex eats" — inherited from animal
Object.getPrototypeOf(dog) === animal; // true
The classic pre-ES5 implementation uses a temporary constructor function:
function F() {}
F.prototype = proto;
return new F();
Examples
Input: ["inherit","world"]
Output: "hello from world"
Input: "null-proto"
Output:
Hints
Classic trick: create an empty constructor function F, set `F.prototype = proto`, return `new F()`.
For `null` proto: you can't use the constructor trick. Use `Object.create(null)` or set `__proto__` directly.
The returned object should have NO own properties — only the prototype link.
Related Problems
Object.create Polyfill
Implement myObjectCreate(proto, propertiesObject) — a polyfill for Object.create that creates a new object with the given prototype.