Object.create method accepts two arguments as:
prototypeObject: newly created objects prototype object. It has to be an object or null.
propertiesObject: Properties of the new object. This argument is optional
Create an object with Object.create with no prototype
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 new property to the person object.
Create object with prototype:
In the above example, we have created a prototypeObject with fullName function. We created a person object with prototypeObject as 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 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
Access descriptors are
In detail, descriptors can be read here
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()
If you enjoyed the article and want updates about my new article, please follow me on medium and on twitter @happyrupesh123
- Beginner’s guide to ReactJS
- 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
- Virtual DOM in ReactJS
- Zip in Python
- decorators in Python
- Concatenating two lists in Python
- lambda, map and filter in Python
- List comprehensions in Python