function methodDecorator(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {...}
function methodDecoratorFactory(...) {
return function(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {...};
}
function methodDecorator(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
console.log('target', target);
console.log('propertyKey', propertyKey);
console.log('descriptor', descriptor);
}
class Test3 {
@methodDecorator
public print() {}
}
// target Test3 { print: [Function] }
// propertyKey print
// descriptor { value: [Function], writable: true, enumerable: true, configurable: true }
target
propertyKey
descriptor
interface PropertyDescriptor {
configurable?: boolean;
enumerable?: boolean;
value?: any;
writable?: boolean;
get?(): any;
set?(v: any): void;
}
function methodDecoratorFactory(canBeEdit: boolean = false) {
return function(
target: any,
propertyKey: string,
descriptor: PropertyDescriptor
) {
descriptor.writable = canBeEdit;
};
}
class Test4 {
@methodDecoratorFactory()
first() {
console.log('first original');
}
@methodDecoratorFactory(true)
second() {
console.log('second original');
}
@methodDecoratorFactory(false)
third() {
console.log('third original');
}
}
const test4 = new Test4();
test4.first = function() {
console.log('first new');
}; // runtime error
test4.second = function() {
console.log('second new');
};// runtime error
test4.third = function() {
console.log('third new');
};
configurable
true
라면 삭제할 수 있다.기본값은 false
.enumerable
true
라면 열거가능하다.기본값은 false
.value