如果您使用getter声明类
class Person {
constructor(firstName, lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
get fullName() {
return [this.firstName, this.lastName].join(" ");
}
}
您可以在实例化新对象后访问getter
const person = new Person("Jane", "Doe");
console.log(person.fullName); // "Jane Doe"
但是使用散布运算符复制对象后,此操作将无效
const personCopy = { ...person };
console.log(personCopy.fullName); // undefined
我认为这有点令人困惑。
答案 0 :(得分:2)
答案 1 :(得分:0)
spread运算符使用Object
作为构造函数创建一个新对象。因此,在您的情况下,personCopy
不是类Person
的实例,因此,它的__proto__
不是Person.prototype
,因此该吸气剂将无法工作