obtain values from an object
set a value of a property within an object
class Book {
constructor(author) {
this._author = author;//_author,惯例命名,表示为私有变量。其实并不能把变量变为私有
}
// getter
get writer() {//get set 是关键字
return this._author;
}
// setter
set writer(updatedAuthor) {//getter、setter 并不是方法,是一个属性
this._author = updatedAuthor;
}
}
const lol = new Book('anonymous');
console.log(lol.writer); // anonymous,获得属性值,即getter
lol.writer = 'wut';//给属性赋值,即setter
console.log(lol.writer); // wut