How to use Object.create() method In Javascript
JavaScript provide us the Object.create() method for creating nested objects allowing us to create new object from an already existing objects, below is an example explaining the creation of nested objects using Object.create() method.
Example: This example shows the creating nested objects using Object.create() method.
Javascript
// Define the parent object const parentObject = { firstProperty: 'w3wiki' , secondProperty: 'A Computer Science Portal' , }; // Creating a child object // with parentObject as its prototype const childObject = Object.create(parentObject); // Adding properties to the child object childObject.thirdProperty = 'Noida' ; //Output parent object and child object console.log( 'Parent Object:' , parentObject); console.log( 'Child Object:' , childObject); |
Output
Parent Object: { firstProperty: 'w3wiki', secondProperty: 'A Computer Science Portal' } Child Object: { thirdProperty: 'Noida' }
How to Create a Nested Object in JavaScript ?
JavaScript allows us to create objects having the properties of the other objects this process is called as nesting of objects. Nesting helps in handling complex data in a much more structured and organized manner by creating a hierarchical structure.
These are the different methods to create nested objects in JavaScript are as follows:
Table of Content
- Using object literals
- Using square bracket notations
- Using factory function
- Using Object.create() method
- Using object constructor
- Using JavaScript classes
Contact Us