我对ECMAScript 6类中getter和setter的重点感到困惑.什么目的?以下是我所指的一个例子:
class Employee {
constructor(name) {
this._name = name;
}
doWork() {
return `${this._name} is working`;
}
get name() {
return this._name.toUpperCase();
}
set name(newName){
if(newName){
this._name = newName;
}
}
}
Run Code Online (Sandbox Code Playgroud)