Open In App

What are Classes in ES6 ?

Classes are the basic building blocks of Object Oriented Programming in any language. In JavaScript, the concept of classes was introduced in ES6 or EcmaScript 2015 with the introduction of the OOPs concept. The JavaScript classes can be used to encapsulate the properties and methods that can be used by creating instances of these classes. A class can be created using the class keyword. It can contain multiple properties and methods using an access specifier to specify their access. The access specifiers can be used as Public, Private, and Protector.

Example: The below code example will show how you can create classes in JavaScript.




class demoClass {
    constructor(name, desc) {
        this.name = name;
        this.desc = desc;
    }
    getData() {
        return {
            name: this.name,
            desc: this.desc
        }
    }
}
const obj =
    new demoClass("GeeksforGeeks",
        "A Computer Science Portal.");
const { name: resObjName, desc: resObjDesc } =
    obj.getData();
console.log(
    "Company Name: ",resObjName,", Description: ",resObjDesc);

Output
Company Name:  GeeksforGeeks , Description:  A Computer Science Portal.
Article Tags :