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 Developer
Example 3: Creating an object without a prototype
const emptyObj = Object.create(null);
console.log(emptyObj.toString); // Output: undefined
Notes
-
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
car
with the propertiesmake
,model
, andyear
usingObject.create()
. The make and model properties should be inherited from anvehicle
object, and theyear
property should be set to the currentyear
using the Date object. -
Create an object
person
with the propertiesfirstName
,lastName
, andage
usingObject.create()
. ThefirstName
andlastName
properties should be inherited from ahuman
object, and theage
property should be set to theage
of the person using the Date object and the person's birthdate. -
Create an object
student
with the propertiesfirstName
,lastName
,age
,major
, andgpa
usingObject.create()
. ThefirstName
,lastName
, andage
properties should be inherited from aperson
object, and themajor
andgpa
properties 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: 2023
Exercise 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: 33
Exercise 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.8
This 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