JavaScript Object.create() and Explicit Prototypes
Quick answer
Object.create constructs an ordinary object with a specified prototype object or null, without invoking a constructor function.
How it works
Object.create(proto) establishes delegation without copying proto’s properties. An optional second argument contains property descriptors, not ordinary values; descriptors are explained in the next lessons. A null-prototype object does not inherit Object.prototype methods, so use static checks such as Object.hasOwn.
const defaults = {tax: 2}; const order = Object.create(defaults);order.price = 8; console.log(order.tax + order.price, Object.keys(order).join(","));Expected output in Node.js:
10 priceOnly price belongs directly to order.
const dict = Object.create(null); dict.code = 7;console.log(Object.getPrototypeOf(dict) === null, Object.hasOwn(dict, "code"));Expected output in Node.js:
true trueA null-prototype object still supports own properties.
Common mistakes
Do not call dictionary.hasOwnProperty on an arbitrary dictionary; that method may be absent or shadowed.
Practice
Create a null-prototype lookup and store a property named toString.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
JavaScript Object.create() and Explicit Prototypes — Knowledge Check
Apply the taught concept and predict the output.
Does Object.create(proto) copy proto’s own properties?
Which is a valid first argument to Object.create?
Why prefer Object.hasOwn for a null-prototype dictionary?
What is the exact console output in JavaScript Object.create() and Explicit Prototypes example 1?
const defaults = {tax: 2}; const order = Object.create(defaults);order.price = 8; console.log(order.tax + order.price, Object.keys(order).join(","));What is the exact console output in JavaScript Object.create() and Explicit Prototypes example 2?
const dict = Object.create(null); dict.code = 7;console.log(Object.getPrototypeOf(dict) === null, Object.hasOwn(dict, "code"));