function logAccess(target: any, propertyKey: string) {
let value: any;
const getter = function () {
console.log(`${propertyKey} 被访问了`);
return value;
};
const setter = function (newValue: any) {
value = newValue;
};
if (delete target[propertyKey]) {
Object.defineProperty(target, propertyKey, {
get: getter,
set: setter,
enumerable: true,
configurable: true
});
}
}
class ExampleClass {
@logAccess
public message: string = 'Hello, World!';
}
const example = new ExampleClass();
console.log(example.message);