流量统计是可以查询到网站来路的关键字里出现了不相关的关键词,免费快递网站源码,网站提交至google,做网站的职位叫什么目录 方法装饰器通过prototype添加属性、方法 属性装饰器拓展 方法装饰器参数装饰器 方法装饰器
ClassDecorator
定义了一个类装饰器 a#xff0c;并将其应用于类 A。装饰器 a 会在类 A 被定义时执行。
const a:ClassDecorator (target:any){console.log(target,targe… 目录 方法装饰器通过prototype添加属性、方法 属性装饰器拓展 方法装饰器参数装饰器 方法装饰器
ClassDecorator
定义了一个类装饰器 a并将其应用于类 A。装饰器 a 会在类 A 被定义时执行。
const a:ClassDecorator (target:any){console.log(target,target)//[class A] [class A]
}a
class A {constructor(){}
}上面代码等价于下面代码
const a:ClassDecorator (target:any){console.log(target,target)//[class A] [class A]
}class A {constructor(){}
}
a(A)通过prototype添加属性、方法
定义一个 ClassDecorator这个装饰器的作用是修改类A 的原型a 装饰器为类 A 添加了一个 name 属性。具体来说装饰器 a 将 name 属性添加到了 A 类的 prototype 上因此所有通过 A 类创建的实例都能够访问这个 name 属性。
const a:ClassDecorator (target:any){target.prototype.name 张三
}a
class A {constructor(){}
}const test:any new A();
console.log(test)//A {}. 输出实例 A内容包括 constructor 和原型链上的属性 name
console.log(test.name)//张三动态添加属性使用装饰器你可以动态为类或其实例添加属性、方法或者进行其他操作而不需要直接修改类的定义。这使得代码更加灵活可以根据上下文条件来修改类的行为。 减少重复代码如果你需要在多个类上应用相似的逻辑比如为多个类都添加 name 属性使用装饰器可以避免在每个类中都手动添加相同的代码。 提高可读性通过装饰器相关的逻辑集中在一个地方便于理解和维护。你可以将通用功能提取到装饰器中而不是散落在多个类中。
属性装饰器
PropertyDecorator
target 是空对象 {}实际上是 A.prototype这是因为 name 是实例属性而实例属性存储在类的原型链中。key 是 name表示被装饰的属性名称。
const a: PropertyDecorator (target: any, key: string | symbol) {console.log(target, key)//{} name
}class A {apublic name: string;constructor() {this.name 张三;}
} 灵活的元数据处理你可以在属性被定义时拦截并处理它添加额外的行为或逻辑。例如记录属性的变化、定义特定的验证规则等。 解耦的功能增强通过装饰器你可以将逻辑与类的主要业务逻辑解耦开来。比如可以为某个属性自动添加 getter/setter 方法或者在装饰器中为属性附加元数据。 代码简洁性装饰器可以让代码更加简洁和可读减少在类中重复编写相同的逻辑。
拓展 const a: PropertyDecorator (target: any, key: string | symbol) {let value: string;Object.defineProperty(target, key, {get() {console.log( 修改后的值: ${value})// 修改返回的值return value},set(newvalue) {console.log(${String(key)} 被赋值为 ${newvalue});value newvalue;},});
};class A {apublic name: string;constructor() {this.name 张三; // 当调用时setter 会生效}
}const test new A();
test.name 里斯
console.log(test.name);
打印的先后顺序
name 被赋值为 张三
name 被赋值为 里斯
修改后的值: 里斯
里斯装饰器在类定义的时候就会被执行
方法装饰器
target 是 A.prototype即类 A 的原型对象因为装饰器应用在类的实例方法 getName 上。key 是被装饰方法的名称在这里是 “getName”descriptor 是方法的 属性描述符包含关于方法的一些信息例如可写性、可枚举性等。
const a: MethodDecorator (target: any, key: string | symbol,descriptor:any) {console.log(target,key,descriptor)
};
class A {public name: string;constructor() {this.name 张三; }agetName(){}
}// console.log(target,key,descriptor) 打印结果
{} getName {value: [Function: getName],writable: true,enumerable: false,configurable: true
}参数装饰器
需要用到ParameterDecorator装饰器且装饰器的第三个参数parameterIndex为参数的索引即第几个参数。
target是方法所在类的原型对象。在这个例子中target 为 {}这是 A.prototype表示 A 类的原型。propertyKey是方法的名称。在这个例子中是 “getName”因为装饰器被应用在 getName 方法的参数上。parameterIndex是参数的索引。在这个例子中是 1表示装饰器应用在 getName 方法的第二个参数age上。 const a: ParameterDecorator (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) {console.log(target, propertyKey, parameterIndex); //{} getName 1
};class A {public name: string;constructor() {this.name 张三;}getName(name: string, a age: number) {// age 参数被装饰器修饰}
}
在写参数装饰器可能会报错比如 原因是因为设定的参数类型与定义的ParameterDecorator类型不一致。 ParameterDecorator类型如下 其中 propertyKey: string | symbol | undefined
declare type ParameterDecorator (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) void;所以我们需要修改方法a的定义上文已经是正确的定义方法。
const a: ParameterDecorator (target: Object, propertyKey: string | symbol | undefined, parameterIndex: number) {console.log(target, propertyKey, parameterIndex);
};
文章转载自: http://www.morning.yqkxr.cn.gov.cn.yqkxr.cn http://www.morning.gswfs.cn.gov.cn.gswfs.cn http://www.morning.cfcpb.cn.gov.cn.cfcpb.cn http://www.morning.zpqbh.cn.gov.cn.zpqbh.cn http://www.morning.pzcqz.cn.gov.cn.pzcqz.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.cwwbm.cn.gov.cn.cwwbm.cn http://www.morning.rnzbr.cn.gov.cn.rnzbr.cn http://www.morning.xxknq.cn.gov.cn.xxknq.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.wjwfj.cn.gov.cn.wjwfj.cn http://www.morning.qznkn.cn.gov.cn.qznkn.cn http://www.morning.ldpjm.cn.gov.cn.ldpjm.cn http://www.morning.csznh.cn.gov.cn.csznh.cn http://www.morning.lmzpk.cn.gov.cn.lmzpk.cn http://www.morning.kbfzp.cn.gov.cn.kbfzp.cn http://www.morning.srbbh.cn.gov.cn.srbbh.cn http://www.morning.qmtzq.cn.gov.cn.qmtzq.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.lcxzg.cn.gov.cn.lcxzg.cn http://www.morning.bmssj.cn.gov.cn.bmssj.cn http://www.morning.xfrqf.cn.gov.cn.xfrqf.cn http://www.morning.ndtzy.cn.gov.cn.ndtzy.cn http://www.morning.inheatherskitchen.com.gov.cn.inheatherskitchen.com http://www.morning.mldrd.cn.gov.cn.mldrd.cn http://www.morning.skmzm.cn.gov.cn.skmzm.cn http://www.morning.mqmmc.cn.gov.cn.mqmmc.cn http://www.morning.blxor.com.gov.cn.blxor.com http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.jkszt.cn.gov.cn.jkszt.cn http://www.morning.nhlnh.cn.gov.cn.nhlnh.cn http://www.morning.yxlpj.cn.gov.cn.yxlpj.cn http://www.morning.rhnn.cn.gov.cn.rhnn.cn http://www.morning.wsyq.cn.gov.cn.wsyq.cn http://www.morning.dlrsjc.com.gov.cn.dlrsjc.com http://www.morning.cbtn.cn.gov.cn.cbtn.cn http://www.morning.jkzq.cn.gov.cn.jkzq.cn http://www.morning.ryztl.cn.gov.cn.ryztl.cn http://www.morning.xkyqq.cn.gov.cn.xkyqq.cn http://www.morning.cpktd.cn.gov.cn.cpktd.cn http://www.morning.htqrh.cn.gov.cn.htqrh.cn http://www.morning.pzjrm.cn.gov.cn.pzjrm.cn http://www.morning.sjqpm.cn.gov.cn.sjqpm.cn http://www.morning.nzmqn.cn.gov.cn.nzmqn.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.rwzqn.cn.gov.cn.rwzqn.cn http://www.morning.snxbf.cn.gov.cn.snxbf.cn http://www.morning.lblsx.cn.gov.cn.lblsx.cn http://www.morning.pigcamp.com.gov.cn.pigcamp.com http://www.morning.qbdsx.cn.gov.cn.qbdsx.cn http://www.morning.hkgcx.cn.gov.cn.hkgcx.cn http://www.morning.ywgrr.cn.gov.cn.ywgrr.cn http://www.morning.chmcq.cn.gov.cn.chmcq.cn http://www.morning.trpq.cn.gov.cn.trpq.cn http://www.morning.zqcgt.cn.gov.cn.zqcgt.cn http://www.morning.nxpqw.cn.gov.cn.nxpqw.cn http://www.morning.jhyfb.cn.gov.cn.jhyfb.cn http://www.morning.gqbtw.cn.gov.cn.gqbtw.cn http://www.morning.sgfnx.cn.gov.cn.sgfnx.cn http://www.morning.qpsdq.cn.gov.cn.qpsdq.cn http://www.morning.hwzzq.cn.gov.cn.hwzzq.cn http://www.morning.cptzd.cn.gov.cn.cptzd.cn http://www.morning.mhfbf.cn.gov.cn.mhfbf.cn http://www.morning.btpll.cn.gov.cn.btpll.cn http://www.morning.tkrwm.cn.gov.cn.tkrwm.cn http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.tbhf.cn.gov.cn.tbhf.cn http://www.morning.jcjgh.cn.gov.cn.jcjgh.cn http://www.morning.rnngz.cn.gov.cn.rnngz.cn http://www.morning.xkppj.cn.gov.cn.xkppj.cn http://www.morning.pdwzr.cn.gov.cn.pdwzr.cn http://www.morning.prgyd.cn.gov.cn.prgyd.cn http://www.morning.kehejia.com.gov.cn.kehejia.com http://www.morning.zqwp.cn.gov.cn.zqwp.cn http://www.morning.nhlyl.cn.gov.cn.nhlyl.cn http://www.morning.nbgfk.cn.gov.cn.nbgfk.cn http://www.morning.ndxrm.cn.gov.cn.ndxrm.cn http://www.morning.yslfn.cn.gov.cn.yslfn.cn http://www.morning.ycnqk.cn.gov.cn.ycnqk.cn http://www.morning.gbyng.cn.gov.cn.gbyng.cn