Export Classes in ECMAScript

Create new Javascript files as below:

Create new Javascript file named human.js. This file contains code demo as below:

class Human {

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

    toString() {
        return 'Name: ' + this.name + '\nGender: ' + this.gender;
    }

}

module.exports = Human;

Create new Javascript file named student.js. This file contains code demo as below:

var Human = require('./human');

class Student extends Human {

    constructor(name, gender, id, score) {
        super(name, gender);
        this.id = id;
        this.score = score;
    }

    toString() {
        return super.toString() + '\nId: ' + this.id + '\nScore: ' + this.score;
    }

}

module.exports = Student;

Create new Javascript file named employee.js. This file contains code demo as below:

var Human = require('./human');

class Employee extends Human {

    constructor(name, gender, id, salary) {
        super(name, gender);
        this.id = id;
        this.salary = salary;
    }

    toString() {
        return super.toString() + '\nId: ' + this.id + '\nSalary: ' + this.salary;
    }

}

module.exports = Employee;




Create new Javascript file named index.js. This file contains code demo as below:

var Student = require('./student');
var Employee = require('./employee');

console.log('Student Info');
let student = new Student("name 1", "male", "st01", 7.8);
console.log(student.toString());

console.log('Employee Info');
let employee = new Employee("name 2", "female", "e01", 123);
console.log(employee.toString());

Use node index.js statement run code demo

Student Info
Name: name 1
Gender: male
Id: st01
Score: 7.8

Employee Info
Name: name 2
Gender: female
Id: e01
Salary: 123