typescript - 为什么实例属性的装饰器在静态属性的装饰器之前执行?
问题描述
我是 TypeScript 的新手。我很好奇为什么实例属性上的装饰器在静态属性上的装饰器之前执行?
这背后有什么原因吗?
考虑下面的代码:
function LogProp(target: any, propertyName: string | Symbol) {
console.log(`Prop Decorator: ${propertyName} is logged`);
}
function LogMethod(
target: any,
name: string | Symbol,
descriptor: PropertyDescriptor
) {
console.log(`Method Decorator: ${name} is logged`);
}
class Book {
@LogProp
static staticProp: string;
@LogProp
instanceProp: string;
constructor(val: string) {
this.instanceProp = val;
}
@LogMethod
instanceMethod() {
return this.instanceProp;
}
@LogMethod
static staticMethod() {
return Math.random();
}
}
输出是:
Prop Decorator: instanceProp is logged
Method Decorator: instanceMethod is logged
Prop Decorator: staticProp is logged
Method Decorator: staticMethod is logged
为什么?
解决方案
推荐阅读
- c# - 如何使用 GridUnitType.Star 在网格长度上执行动画?
- php - 在 php 7+ 中使用 sqlsrv 执行存储过程
- python - 如何通过计数过滤熊猫 DF 并同时平均组?
- sas - SAS dsnless 通过传入凭据连接到 SQL Server
- javascript - 数组中的元素自动更新
- validation - 如何从 Hybris 1905 中删除 CustomerName 验证,这样当我创建一个客户时,我可以在没有名称的情况下执行它?
- c++ - 选择排序算法产生乱序结果
- c# - VB .Net 无法在非托管 DLL 的方法中引用 byte* 指针
- transparency - 使用 Geopandas 绘制 shapefile 时 Alpha 透明度不均匀
- json - fullcalendar dayClick 函数在 ajax 调用 json 数据中不起作用