Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to make a class in a another class in JavaScript?

I want to make something like bla.FooBarBaz in JavaScript, but, bla is a class and FooBarBaz is a class, but INSIDE THE bla CLASS. How to do it?

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

There are no nested classes in ES6 but yes, you can put a second class as a static property on another class, like this:

class Parent {
    //code
}
Parent.B = class {
   //code
};

or by using extra scope:

var dataClass;
{
    class D {
        constructor() { }
    }
    dataClass = class DataClass {
        constructor() { }
        method() {
            var a = new D();  // works fine
        }
    }
}

But with the help of this proposed class, you can write a single expression or declaration:

class Parent {
    //code
    static Child = class {
         //code
    }
};
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading