FrontendAtlas
Interview Questions
Home>JavaScript interview questions> JavaScript coding challenges >Create Object with Prototype

Create Object with Prototype

easy
By FrontendAtlas Editorial · Updated Jan 30, 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.
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

Solution

Overview

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.

1

Approach: Object.create + Object.assign

Steps:

1) Validate that proto is an object/function or null; otherwise throw a TypeError.

2) If using the classic pattern, define a function F, set F.prototype = proto, and return new F().

3) If proto is null, ensure the created object has no prototype.

Complexity: O(1) time and space.

export default function createObject(proto, props) {
  const validProto = proto === null || typeof proto === 'object' || typeof proto === 'function';
  if (!validProto) throw new TypeError('proto must be an object or null');

  const obj = Object.create(proto);
  if (props != null) {
    if (typeof props !== 'object') throw new TypeError('props must be an object');
    Object.assign(obj, props);
  }
  return obj;
}
export default function createObject(proto: object | null, props?: Record<string, any>): object {
  const validProto = proto === null || typeof proto === 'object' || typeof proto === 'function';
  if (!validProto) throw new TypeError('proto must be an object or null');

  const obj = Object.create(proto);
  if (props != null) {
    if (typeof props !== 'object') throw new TypeError('props must be an object');
    Object.assign(obj, props);
  }
  return obj;
}

Notes & Pitfalls

Pitfalls
  • Avoid assigning to `__proto__` directly; use `Object.create`.
  • If `props` is `null` or non-object, decide whether to ignore or throw (here we throw).
  • Objects created with `null` prototype do not inherit `toString`, `hasOwnProperty`, etc.
Edge cases
  • `proto = null` should create a null-prototype object.
  • `props` omitted should still create an object with the right prototype.
Techniques
  • `Object.create` for prototype wiring.
  • `Object.assign` for shallow property copy.

Common mistakes on this challenge

  • Prototype wiring or validation is incorrect

    Object creation should support valid prototypes (including null) and reject invalid ones.

    • Validate `proto` is object/function/null before creation.
    • Create with `Object.create(proto)` to set prototype correctly.
    • Copy optional props after creation and guard invalid props input.

Resources

  • MDN – Object.create()
  • MDN – Object.getPrototypeOf()

Similar questions

Implement `instanceof`intermediateImplement the `new` OperatorhardShallow Clone (Object or Array)easy

Guides

Frontend interview preparation guideGuideFrontend coding interview questions and prep guideBlueprintJavaScript Problems That Actually Show UpBlueprintBuild Great UI in 60 MinutesBlueprint

Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.

Open frontend interview questionsBrowse JavaScript interview questionsOpen Essential 60Open Machine Coding HubOpen Frontend Coding ChallengesOpen System DesignOpen Interview Prep GuideOpen System Design BlueprintOpen Framework Prep PathsOpen Study PlansOpen JavaScript mastery study planOpen Company PrepOpen JavaScript Framework Prep Guide
↗Incidents hub
← Prev←46 / 88