Getter and Setter in ECMAScript

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

class Student {

    get id() {
        return this._id;
    }
    set id(value) {
        this._id = value;
    }

    get name() {
        return this._name;
    }

    set name(value) {
        this._name = value;
    }

    toString() {
        return 'Id: ' + this.id + '\nName: ' + this.name;
    }

}

let student = new Student();
student.id = 'st01';
student.name = 'Name 1';
console.log('Student Info');
console.log(student.toString());




Use node index.js statement run code demo

Student Info
Id: st01
Name: Name 1