Is it possible to use “Class” in Node.js?

Technology CommunityCategory: Node.jsIs it possible to use “Class” in Node.js?
VietMX Staff asked 3 years ago

With ES6, you are able to make “actual” classes just like this:

class Animal {

    constructor(name) {
        this.name = name;
    }

    print() {
        console.log('Name is :' + this.name);
    }
}

You can export a class just like anything else:

module.exports = class Animal {

};

Once imported into another module, then you can treat it as if it were defined in that file:

var Animal = require('./Animal');

class Cat extends Animal {
    ...
}