class Person {
constructor(name) {
this._name = name;
}
get name() {
return this._name.toUpperCase();
}
set name(newName) {
this._name = newName;
}
}
let newPerson = new Person("mike")
getter 和 setter 有什么好处?我还不能通过 newPerson._name
获得它并获得 mike
吗?要设置它,我不能只执行 newPerson._name = "Phil"
来设置它吗?
答案 0 :(得分:2)
查看 getter 应该会给你一个提示:
get name() {
return this._name.toUpperCase();
}
它可以让您执行可能很复杂的逻辑,伪装成简单的属性查找。再举一个例子,考虑如果你有 firstName
和 lastName
属性 - 那么你可以让 name
成为实际基础属性的 getter/setter:
class Person {
constructor(name) {
// Invoke the setter below
this.name = name;
}
get name() {
return this._firstName + ' ' + this._lastName;
}
set name(newName) {
const [first, last] = newName.split(' ');
this._firstName = first;
this._lastName = last;
}
}
let newPerson = new Person("John Smith")
console.log(newPerson.name);
如果没有 getter/setter,这种功能(运行逻辑并返回自定义的东西而不是只返回普通的属性值)是不可能的。
也就是说,对于您的原始代码,除了 toUpperCase
部分,是的,getter/setter 并没有真正为您做很多事情。
我不能只做 newPerson._name = "Phil" 来设置它吗?
是的,你可以。下划线通常用于表示一个属性不应该在类的外部使用,但它们不会完全禁止。如果您想让属性真正私有,请使用 #
私有字段语法。
class Person {
#firstName;
#lastName;
constructor(name) {
// Invoke the setter below
this.name = name;
}
get name() {
return this.#firstName + ' ' + this.#lastName;
}
set name(newName) {
const [first, last] = newName.split(' ');
this.#firstName = first;
this.#lastName = last;
}
}
let newPerson = new Person("John Smith")
console.log(newPerson.name);
答案 1 :(得分:0)