[英]How to reassign an objects “getter” method?
Lets say theres an object with a getter method. 可以说有一个带有getter方法的对象。
var person = {
name : "John",
get title(){
return "Dr." + this.name
}
}
>>>person.title
>>>"Dr.John"
I would like to reassign the getter method. 我想重新分配getter方法。
person.title = function (){return "Mr." + this.name}
Is there any way? 有什么办法吗? Or at least create one dynamically or similar workarounds?
或者至少创建一个动态或类似的解决方法?
You can use Object.defineProperty 您可以使用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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.