Object.create()
Object.create() is a method that allows you to create a new object with a specified prototype object and optionally, additional properties.
Syntax
Object.create(proto);
Object.create(proto, propertiesObject);Parameter
proto: The object that will be the prototype of the newly created object.propertiesObject(optional): An object containing properties to be added to the newly created object. Each property descriptor is defined as a key-value pair. The key is the property name and the value is an object that describes the property.
If proto is null or undefined, Object.create() will create a new object with no prototype.
The propertiesObject argument is optional. If it is not provided, the newly created object will have no properties other than those inherited from its prototype.
propertiesObject can be used to define properties using property descriptors.
Return Value
Object.create() returns a new object with the specified prototype object and properties.
Examples
Example 1: Creating an object with a specified prototype
const animal = {
type: "animal",
describe() {
console.log(`I am a ${this.type}.`);
},
};
const dog = Object.create(animal);
dog.type = "dog";
dog.describe(); // Output: I am a dog.Example 2: Creating an object with additional properties
const person = {
firstName: "John",
lastName: "Doe",
};
const employee = Object.create(person, {
jobTitle: { value: "Software Developer" },
salary: { value: 50000 },
});
console.log(employee.firstName); // Output: John
console.log(employee.jobTitle); // Output: Software DeveloperExample 3: Creating an object without a prototype
const emptyObj = Object.create(null);
console.log(emptyObj.toString); // Output: undefinedNotes
-
The prototype chain can be as long as you need it to be. You can create an object with a prototype, and then create another object with the first object as its prototype, and so on.
-
The
Object.create()method is often used in conjunction with theObject.defineProperty()method to create objects with specific properties and attributes. -
When using
Object.create()to create an object with a prototype, any changes made to the properties of the prototype object will affect all objects that inherit from it. -
Object.create()is often used as an alternative to the new keyword and constructor functions for creating objects in JavaScript. -
Object.create()is more flexible than using the new keyword and constructor functions because it allows you to create objects with any prototype, not just those created by constructor functions.
Exercises
-
Create an object
carwith the propertiesmake,model, andyearusingObject.create(). The make and model properties should be inherited from anvehicleobject, and theyearproperty should be set to the currentyearusing the Date object. -
Create an object
personwith the propertiesfirstName,lastName, andageusingObject.create(). ThefirstNameandlastNameproperties should be inherited from ahumanobject, and theageproperty should be set to theageof the person using the Date object and the person's birthdate. -
Create an object
studentwith the propertiesfirstName,lastName,age,major, andgpausingObject.create(). ThefirstName,lastName, andageproperties should be inherited from apersonobject, and themajorandgpaproperties should be set usingpropertiesObject.
Solutions
Exercise 1:
fconst vehicle = {
make: 'Toyota',
model: 'Camry'
};
const car = Object.create(vehicle, {
year: { value: new Date().getFullYear() }
});
console.log(car.make); // Output: Toyota
console.log(car.year); // Output: 2023Exercise 2:
const human = {
firstName: 'John',
lastName: 'Doe'
};
const person = Object.create(human, {
age: { value: () => {
const birthdate = new Date('1990-01-01');
const now = new Date();
const diff = now - birthdate;
const ageDate = new Date(diff);
return Math.abs(ageDate.getUTCFullYear() - 1970);
}}
});
console.log(person.firstName); // Output: John
console.log(person.age()); // Output: 33Exercise 3:
const person = {
firstName: 'John',
lastName: 'Doe',
age: 33
};
const student = Object.create(person, {
major: { value: 'Computer Science' },
gpa: { value: 3.8 }
});
console.log(student.firstName); // Output: John
console.log(student.major); // Output: Computer Science
console.log(student.gpa); // Output: 3.8This page was updated on -
Found an error or have feedback on our docs?
Create an issue on GitHub and let us know! Your input helps improve our documentation for everyone in the community.
Report error, send feedback on Github