shabashj
3/29/2017 - 8:41 AM

ES6 getter and setter how to.

ES6 getter and setter how to.

class Animal {
    constructor(name) {
        this._name = name;
    }

    get name() {
        return 'Animal name is ' + this._name;
    }

    set name(name) {
        this._name = name + '!';
    }
}

let cat = new Animal('Cat');
cat.name = 'Kitty'
console.log(cat.name);