面试题答案
一键面试function propertyListener(target: any, propertyKey: string) {
let value: any;
const descriptor: PropertyDescriptor = {
get() {
return value;
},
set(newValue) {
if (newValue!== value) {
value = newValue;
console.log(`新值: ${newValue}`);
}
}
};
return descriptor;
}
class User {
@propertyListener
name: string;
constructor(name: string) {
this.name = name;
}
}
// 测试
const user = new User('Alice');
user.name = 'Bob';