The Wayback Machine - https://web.archive.org/web/20240717024439/https://www.geeksforgeeks.org/javascript-nested-classes/
Open In App

JavaScript Nested Classes

Last Updated : 20 Feb, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Let us try to understand, what is class. A class in JavaScript is a type of function, which can be initialized through both function keywords as well as through class keywords. In this article, we will cover the inner class in javascript through the use of a function keyword. Here’s an example of the class using the function keyword. 

Example: 

html




<script>
    // Write Javascript code here
    function Employee() {
          
        // Here we have created a property
        // name of class Employee;
        this.name = "";
        this.employeeId = 0;
    }
      
    // An (global) object of type Employee
    // is created.
    var emp = new Employee();
      
    emp.name = "miss anonymous";
    emp.employeeId = 101;
    function showName() {
        document.getElementById("gfg").innerHTML = emp.name;
    }
</script>
<button type="button" onclick="showName()">
    Click Me
</button>
<p id="gfg"></p>


Output:

JavaScript  Nested Classes

JavaScript  Nested Classes

The difference between class as a function and function is that we make use of this keyword in a function definition that makes it work as a class. Now, an inner class is defined in a similar way. As you can see in this code, we have created an OuterClass and InnerClass in it just the same way as we did before. But, to access the properties of the outer class we need to have the address of the object. And that is the only right way to do it. As you can see in the example above, in order to access the property x of OuterClass we have used the variable objOuterClass (which stores the address of the current instance of the class) and using that address of object we can easily access any property or method of the outer class in the inner class. Now, the same trick can be applied while trying to access the members or properties of the inner class. As you can see, we have created an inner class object outside the inner class. So every time the object of Outer Class is created we are creating the object of inner class ad storing its address in the variable innerObj. 

Example: 

html




<script>
    function OuterClass() {
          
    // Property x of OuterClass
    this.x = 0;
    this.y = 30;
          
    // Assign the variable objOuterClass the address
            // of the particular instance of OuterClass
    var objOuterClass = this;
          
    // Inner class
    function InnerClass() {
    this.z = 10;
    this.someFuncton = function () {
        alert("inner class function");
            };
              
    // Assign the address of the anonymous function.
    // to access the value of property of outer class
    // in inner class.
    this.accessOuter = function () {
                        document.getElementById("gfg")
                                .innerHTML="value of x property:" + objOuterClass.x;
            };
    }
          
        // InnerClass ends to access the inner class
        // properties or methods.
        this.innerObj = new InnerClass();
    }
      
            function show() {
    var outerClassObj = new OuterClass();
    document.getElementById("gfg")
            .innerHTML = "Inner class Property z:" + outerClassObj.innerObj.z;
    }
</script>
<button type="button" onclick="show()">
    Click Me
</button>
<p id="gfg"></p>


Output:

JavaScript  Nested Classes

JavaScript  Nested Classes



Similar Reads

How to Create Nested Classes in TypeScript ?
In TypeScript, you can create nested classes using different methods. We will discuss about three different approaches to creating nested classes in TypeScript. These are the approaches: Table of Content By defining nested classes inside a classBy using the namespacesBy using the modulesBy defining nested classes inside a classIn this approach, we
3 min read
Tailwind CSS 3 Classes doesn't Override Previous Classes
Tailwind CSS is basically a Utility-first CSS framework for building rapid custom UI. It is a highly customizable, low-level CSS framework that gives you all of the building blocks that you need. Also, it is a cool way to write inline styling and achieve an awesome interface without writing a single line of your own CSS. Syntax&lt;div class="bg-blu
2 min read
How to break nested for loop using JavaScript?
The break statement, which is used to exit a loop early. A label can be used with a break to control the flow more precisely. A label is simply an identifier followed by a colon(:) that is applied to a statement or a block of code. Note: there should not be any other statement in between a label name and associated loop. Example-1: Break from neste
3 min read
How to filter nested JSON object to return certain value using JavaScript ?
Given a collection that contains the detail information of employees who are working in the organization. We need to find some values from that nested collections of details. That collection is known as the JSON object and the information inside object are known as nested JSON object. Example 1: We create the nested JSON objects using JavaScript co
4 min read
How to append new information and rethrowing errors in nested functions in JavaScript ?
In this article, we will see how we may append new information and rethrow errors in nested functions in JavaScript with the help of certain theoretical explanations &amp; understand them through the illustrations. This article is divided into 2 sections, i.e. in the 1st section, we'll understand how to append the new information while throwing an
5 min read
How to create an array with multiple objects having multiple nested key-value pairs in JavaScript ?
In this article, we will learn to create an array with multiple objects having key-value pairs, we will use nesting which means defining another array in a pre-defined array with key-value pairs. Suppose we have several arrays of objects means containing various key-value pairs where keys are uniquely identified now we have to add these arrays to a
3 min read
Design a Nested Chat Comments using HTML CSS and JavaScript
In this article, we will learn how to create Nested Comments using JavaScript. We must have seen it on social media like Facebook, Instagram, Youtube, Twitter, etc. We have seen the use of the nested comment in the comment section of these social sites. Approach: Create a folder nested-comments on your local machine and go to that folder.Create 3 f
2 min read
JavaScript Nested functions
In JavaScript, Functions within another function are called “Nested function.” These nested functions have access to the variables and parameters of the outer (enclosing) function, creating a scope hierarchy. A function can have one or more inner functions. Syntax:// Outer function function outerFunction() { // Nested function function nestedFuncti
2 min read
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 Co
4 min read
How to filter Nested Array using Key in JavaScript ?
Filtering a nested array in JavaScript involves the process of selectively extracting elements from an array of objects, where each object contains another array. We will discuss how can we filter Nested Array using the key in JavaScript. Filtering a nested array based on a key in JavaScript can be done using various methods: Table of Content Using
3 min read
three90RightbarBannerImg