DEV Community

Sujith V S
Sujith V S

Posted on

New generation syntax for JavaScript classes | ES6 / Babel

A class mainly have properties and methods.
Properties are like 'Variables attached to classes'.
Methods are like 'functions attached to classes'.

Let's take a look at a class which has the new gen syntax,

class Human {
    gender = 'male';
    printGender = () => {
        console.log(this.gender);
    }
}

class Person extends Human {
    name = "Max";
    printMyName = () => {
        console.log(this.name);
    }
}
Enter fullscreen mode Exit fullscreen mode

Here we exclude the constructor function and directly assign the value to a variable inside the class.
And then we use arrow functions syntax to create method inside the class.

Top comments (0)