如何重新分配对象 "getter" 方法?

How to reassign an objects "getter" method?

假设有一个带有 getter 方法的对象。

var person = {
 name : "John",
 get title(){
 return "Dr." + this.name
 }
}
>>>person.title
>>>"Dr.John"

我想重新分配 getter 方法。

person.title = function (){return "Mr." + this.name}

有什么办法吗? 或者至少创建一个动态或类似的解决方法?

您可以使用Object.defineProperty

var person = {
 name: "John",
 get title() {
  return "Dr." + this.name
 }
}

console.log(person.title)

Object.defineProperty(person, "title", {
 get: function() {
  return "Mr." + this.name
 }
});

console.log(person.title)