Learn all about the create() method of the Object object in JavaScript.

Introduced in ES5, the create() method is used to create a new object with a specified prototype.

Usage

To use the create() method, follow this syntax:

const newObject = Object.create(prototype);

For example:

const animal = {};
const dog = Object.create(animal);

The newly created object will inherit all the properties of the prototype object.

You can also add new properties to the object that the prototype lacked by specifying a second parameter:

const newObject = Object.create(prototype, newProperties);

Here, newProperties is an object of objects that define each property.

For example:

const animal = {};
const dog = Object.create(animal, {
  breed: {
    value: 'Siberian Husky'
  }
});
console.log(dog.breed); //'Siberian Husky'

Note that you must pass a property descriptor object ({ value: 'Siberian Husky' }) instead of just assigning a value directly (breed: 'Siberian Husky').

The create() method is often used in combination with Object.assign():

const dog = Object.assign(Object.create(animal), {
  bark() {
    console.log('bark');
  }
});

By using Object.create() along with Object.assign(), you can create a new object that inherits properties from a prototype and add new properties or methods to the object.