Object.create in JavaScript
The Object.create
method is one of the methods to create a new object in JavaScript.
Other methods of creating objects in JavaScript are described in my previous article. I recommend reading my previous articles on Understanding Prototypes and Inheritance to get the best out of this article.
Basic syntax:
Object.create(prototype_object, propertiesObject)
Object.create
method accepts two arguments as:
prototypeObject
: Newly created object’s prototype object. It has to be an object ornull
.propertiesObject
: Properties of the new object. This argument is optional
Create an object with Object.create with no prototype
Consider the below example to create a new object in JavaScript
Here, we have created a new object person using Object.create
method. As we have passed null
for the prototypeObject
. person
object does not have any prototype object.
Further, we have added name
as a new property to the person object.
Create an object with prototype:
Console output:
In the above example, we have created a propertiesObject
with fullName
function. We created a person
object with propertiesObject
as a prototype object of the person’s
object using Object.create
. Further, we added firstName
and lastName
properties to the person
object. Here, we have added firstName
and lastName
properties after the object creation. It would have been great if we could add these properties while creating the object. To do that, we will use the 2nd argument of Object.create
method.
Object.create 2nd argument — propertiesObject
propertiesObject
is used to create properties on a new object. It acts as a descriptor for the new properties to be defined. Descriptors can be data descriptor or access descriptors.
Data descriptors are
- configurable
- enumerable
- value
- writable
Access descriptors are
- get
- set
In detail, descriptors can be read here
Example:
In the above example, we have created a new object person with prototype object as prototypeObject
and properties as firstName
and lastName
.
Properties firstName and lastName have been added using the 2nd parameter of the Object.create().
Inheritance using Object.create()
Read Inheritance in JavaScript before reading the below part.
Here we have copied the prototype of the SuperType to the SubType.prototype using. Object.create
method. Rest everything is same as the inheritance in JavaScript.
If you like my articles and find them useful, feel free to buy me a coffee. Thanks!
To get updates for my new stories, follow me on medium and twitter
Other articles:
- JavaScript Internals: JavaScript engine, Run-time environment & setTimeout Web API
- Beginner’s guide to ReactJS
- The Journey of JavaScript: from Downloading Scripts to Execution
- Why Progressive Web Apps are great and how to build one
- Let’s get this ‘this’ once and for all
- Service Workers
- Service Workers implementation
- Execution Context in JavaScript
- Virtual DOM in ReactJS
- Prototypes in JavaScript
- ‘this’ in JavaScript
- Object.create in JavaScript
- Inheritance in JavaScript
- Create objects in JavaScript
- Objects in JavaScript
- Zip in Python
- decorators in Python
- Concatenating two lists in Python
- lambda, map, and filter in Python
- List comprehensions in Python