How to use object constructor In Javascript
JavaScript allows us to create the nested objects using constructor function which work as the initial starting point for the creation of the objects with the specific properties and behaviour. It can be used defining the each level of the nested objects hierarchy.
Example: This example shows the creating nested Object using object constructor.
Javascript
function company(name, location, courseObj) { this .name = name; this .location = location; this .course = courses; } function course(courseName) { this .courseName = courseName; } let courses = new course( "DSA self paced course" ) let companyName = new company( "GeekforGeeks" , "Noida" , courses); console.log(companyName); |
Output
company { name: 'GeekforGeeks', location: 'Noida', course: course { courseName: 'DSA self paced course' } }
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