Skip to content

Object Create ( ) Method

Object.create(proto) makes a new, empty object whose prototype is proto. When the new object is asked for a property it does not have, JavaScript looks for it on proto, so the new object inherits everything proto holds without copying it. It is the most direct way to set up inheritance between plain objects, with no constructor function or class involved.

const account = {
currency: 'GBP',
describe() {
return `${this.owner} has ${this.balance} ${this.currency}`;
},
};
const savings = Object.create(account);
savings.owner = 'Tom';
savings.balance = 250;
console.log(savings);
console.log(savings.describe());
console.log(Object.hasOwn(savings, 'currency'));
console.log(Object.getPrototypeOf(savings) === account);

This prints:

{ owner: 'Tom', balance: 250 }
Tom has 250 GBP
false
true

Logging savings shows only its own properties, yet savings.describe() and savings.currency both work, because they are found on account. Object.hasOwn() confirms that currency is inherited rather than own. The Prototype Chain explains the lookup in full.

The new object keeps a link to its prototype rather than a snapshot of it. A change to the prototype shows through every object made from it, until an object sets its own property of the same name, which then hides the inherited one:

const account = { currency: 'GBP' };
const savings = Object.create(account);
const holiday = Object.create(account);
account.currency = 'EUR';
holiday.currency = 'USD';
console.log(savings.currency, holiday.currency, account.currency);

This prints:

EUR USD EUR

Setting holiday.currency created an own property on holiday, and it did not change account.

An optional second argument defines the new object’s own properties, using the same property descriptors as Object.defineProperties(). Any attribute left out of a descriptor defaults to false, so a property defined this way is read-only, hidden from loops and impossible to delete unless the descriptor says otherwise:

const account = { currency: 'GBP' };
const isa = Object.create(account, {
owner: { value: 'Ana', enumerable: true },
limit: { value: 20000 },
});
isa.owner = 'Someone else';
console.log(isa);
console.log(isa.owner, isa.limit);
console.log(Object.getOwnPropertyDescriptor(isa, 'limit'));

This prints:

{ owner: 'Ana' }
Ana 20000
{
value: 20000,
writable: false,
enumerable: false,
configurable: false
}

The assignment to owner was ignored because owner is not writable, and in strict mode it would throw a TypeError instead. limit does not appear when the object is logged, because it is not enumerable, but it can still be read. Property Descriptors covers each attribute.

Object.create(null) makes an object with no prototype at all, so it inherits nothing, not even toString or hasOwnProperty. That makes it a clean dictionary, where any string can be used as a key without clashing with a built-in name:

const plain = {};
const bare = Object.create(null);
console.log('toString' in plain, 'toString' in bare);
bare.toString = 'just a key';
console.log(bare);

This prints:

true false
[Object: null prototype] { toString: 'just a key' }

Because such an object has no hasOwnProperty method, use Object.hasOwn(obj, key) to test it, which works on every object.

Steve Griffith covers this topic in his video JavaScript Object create method.