Create Object with Prototype
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 — 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.
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; // trueSolution
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.
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
Avoid assigning to __proto__directly; useObject.create.If propsisnullor non-object, decide whether to ignore or throw (here we throw).Objects created with nullprototype do not inherittoString,hasOwnProperty, etc.
proto = nullshould create a null-prototype object.propsomitted should still create an object with the right prototype.
Object.createfor prototype wiring.Object.assignfor 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 protois 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
Similar questions
Guides
Preparing for interviews? Use Frontend Coding Challenges first, then move into a concrete Study Plan before targeted Company Prep.
Built in public
The code behind FrontendAtlas lives on GitHub. A star helps other frontend engineers find it.