JavaScript Nested Classes Last Updated : 20 Feb, 2023 Comments Improve Suggest changes Like Article Like 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 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 Comment More infoAdvertise with us Next Article JavaScript Nested Classes M miss_anonymous Follow Improve Article Tags : JavaScript Web Technologies javascript-oop Similar Reads Nested Classes in Java In Java, it is possible to define a class within another class, such classes are known as nested classes. They enable you to logically group classes that are only used in one place, thus this increases the use of encapsulation and creates more readable and maintainable code. The scope of a nested cl 5 min read Different Types of Classes in Java with Examples A class is a user-defined blueprint or prototype from which objects are created.  It represents the set of properties or methods that are common to all objects of one type. In general, class declarations can include these components, in order:  Modifiers: A class can be public or has default access 11 min read Java - Inner Class vs Sub Class Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMe 3 min read Difference Between Static and Non Static Nested Class in Java Nested classes are divided into two categories namely static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. A class can either be static or non-static in java. So there is a lot of difference between makin 4 min read Classes and Objects in Java In Java, classes and objects are basic concepts of Object Oriented Programming (OOPs) that are used to represent real-world concepts and entities. The class represents a group of objects having similar properties and behavior, or in other words, we can say that a class is a blueprint for objects, wh 11 min read Static class in Java Java allows a class to be defined within another class. These are called Nested Classes. Classes can be static which most developers are aware of, henceforth some classes can be made static in Java. Java supports Static Instance Variables, Static Methods, Static Block, and Static Classes. The class 3 min read Anonymous Inner Class in Java Nested Classes in Java is prerequisite required before adhering forward to grasp about anonymous Inner class. It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain "extras" such as o 7 min read Inner Class in Java In Java, inner class refers to the class that is declared inside class or interface which were mainly introduced, to sum up, same logically relatable classes as Java is object-oriented so bringing it closer to the real world. Now geeks you must be wondering why they were introduced? There are certai 11 min read Types of Classes in Java A class is a blueprint in the Java programming language from which an individual object can be built. In Java, we may declare a class by using the class keyword. Class members and functions are declared simply within the class. Classes are required for the creation of Java programs. The object-orien 8 min read Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an 13 min read Like