使用 `属性` 装饰器修改实例变量
Modify the instance variable using `property` decorator
全部,
是否可以使用property
装饰器修改实例变量?
例如,
const config = [
a: { },
b: { },
];
class Test {
@fetchObjectKey('a')
public message; // Should fetch the value against the `key` supplied & assign it to `instance` ?
//...
}
// You get better auto completion when its casted as const
const config = <const>{
a: { foo: 1 },
b: { foo: 2 }
};
function fetchObjectKey(key: keyof typeof config) {
return (target: object, propertyKey: string) => {
Reflect.set(target, propertyKey, config[key].foo);
};
}
// set property based on argument
class Test {
@fetchObjectKey("a")
public message!: number;
}
console.log(new Test().message); // will be 1
全部,
是否可以使用property
装饰器修改实例变量?
例如,
const config = [
a: { },
b: { },
];
class Test {
@fetchObjectKey('a')
public message; // Should fetch the value against the `key` supplied & assign it to `instance` ?
//...
}
// You get better auto completion when its casted as const
const config = <const>{
a: { foo: 1 },
b: { foo: 2 }
};
function fetchObjectKey(key: keyof typeof config) {
return (target: object, propertyKey: string) => {
Reflect.set(target, propertyKey, config[key].foo);
};
}
// set property based on argument
class Test {
@fetchObjectKey("a")
public message!: number;
}
console.log(new Test().message); // will be 1