Create Object with Prototype

easy
By FrontendAtlas Editorial · Updated Oct 10, 2026
Implement objectCreate(proto) to return a new object whose prototype is proto. Use a temporary constructor function or Object.create-like pattern and handle null prototypes safely. This tests understanding of prototypes and inheritance without mutating __proto__.

Arguments

  • proto: Object | null — The prototype for the new object.
  • props: Object (optional) — Own enumerable properties to copy onto the new object.

Returns

Object — A new object with the specified prototype and properties.

Solution idea

Create a new object with its internal [[Prototype]] set to the given proto. You can do this by temporarily assigning proto to a function’s prototype and instantiating it, or by using the Object.create pattern. Guard against invalid prototypes and support null when required.

Examples
const proto = { greet() { return 'hi'; } };
const obj = createObject(proto, { name: 'Ada' });
Object.getPrototypeOf(obj) === proto; // true
obj.name; // 'Ada'
obj.greet(); // 'hi'

const bare = createObject(null);
Object.getPrototypeOf(bare) === null; // true