How to use factory function In Javascript
We can also create nested objects in javaScript by using factory function so as to define the objects and their organised nested structure.
Example: This example shows creating nested objects using factory function.
Javascript
function objects() { let nestedObjects = { obj: { Company: { name: "w3wiki" } } } return nestedObjects; } console.log(objects()); |
{ obj: { Company: { name: 'w3wiki' } } }
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