我可以在这样的
JavaScript对象中得到getter
var member = { firstName:"XYZ",lastName:"zzz",get fullName(){ return (this.firstName + ' ' + this.lastName); } }
member.isGuest = true;
但是有什么办法可以将getter添加到已经存在的对象中吗?这样的东西
member.isGuest = get isGuest(){ return this.firstName=='Guest';}
解决方法
尝试defineProperty
Object.defineProperty(member,'isGuest',{ get: function() { return this.firstName=='Guest' } });