Open EdwardZZZ opened 7 years ago
装饰器 顺序 解释顺序,执行倒序,先方法再类
class C {
@f()
@g()
method() {}
}
f(): evaluated
g(): evaluated
g(): called
f(): called
autobind
function d(target: any, propertyKey: string, { value, configurable, enumerable }: PropertyDescriptor) {
return {
configurable,
enumerable,
get() {
return function (...props: any[]) {
console.log('before');
value.apply(this, props);
console.log('after');
}.bind(this);
},
};
}
function D(target, key, desc) {
const { value, configurable, enumerable, writable } = desc;
return {
configurable,
enumerable,
writable,
value: 2
}
}
function D1(target, key, desc) {
desc.value = function () {
console.log('call b');
}
Reflect.defineProperty(target, key, desc);
}
class Test {
@D
a = 1
@D1
b() {
console.log('bbbbbb');
}
}
const t = new Test();
console.log(t.a);
t.b();
当修饰器带参数时,
当修饰器不带参数时,