当前位置: 首页 > news >正文

安徽网站建设公司品牌营销与推广

安徽网站建设公司,品牌营销与推广,城阳做网站,微商系统软件开发定义 装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,访问符,属性或参数上,是一种在不改变原类和使用继承的情况下,动态的扩展对象功能。 装饰器使用expression形式,其中expression必须…

定义

装饰器是一种特殊类型的声明,它能够被附加到类声明,方法,访问符,属性或参数上,是一种在不改变原类和使用继承的情况下,动态的扩展对象功能。
装饰器使用@expression形式,其中expression必须评估为一个函数,该函数将在运行时调用,并带有有关装饰声明的信息。
前置操作

// tsconfig.json
{"compilerOptions": {"target": "ES5","experimentalDecorators": true}
}

使用

类装饰

类装饰器会把Class A的构造函数传入你的watcher函数当做第一个参数

const watcher: ClassDecorator = (target) => {target.prototype.age = 18target.prototype.getName = ():string=>{return 'hello'}
}
@watcher
class A {constructor() {}
}
const a = new A()
console.log((a as any).age);  // 18
console.log((a as any).getName());  //hello

装饰器工厂

如果想要传递参数,使装饰器变成类似工厂函数,只需要在装饰器函数内部再返回一个函数即可。

const watcher = (name: string): ClassDecorator => {return (target) => {target.prototype.age = 18target.prototype.getName = (): string => {return name}}}
@watcher('hello data')
class A {constructor() {}
}
const a = new A()
console.log((a as any).age); // 18
console.log((a as any).getName()); // hello data

装饰器组合

可以给一个类使用多个装饰器

const watcher = (name: string): ClassDecorator => {return (target) => {target.prototype.age = 18target.prototype.getName = (): string => {return name}}}const addAge = (age:number):ClassDecorator => {return (target) =>{target.prototype.addAge = (): number => {return age+1}}
}@watcher('hello data')  @addAge(20)
class A {constructor() {}
}
const a = new A()
console.log((a as any).age);
console.log((a as any).getName());
console.log((a as any).addAge());

方法/属性装饰器

装饰器参数为:

  • target:对象的原型:对于静态成员来说是类的构造函数,对于实例成员是类的原型对象
  • propertyKey:方法的名称
  • descriptor:方法的属性描述符
    这三个属性实际就是Object.defineProperty的三个参数,如果是类的属性,则没有传递第三个参数
const met: MethodDecorator = (...args) => {console.log(args)
}class A {constructor() {}@metgetName(): string {return 'hello'}
}const xx = new A()

在这里插入图片描述

// 声明装饰器修饰方法/属性
function method(target: any, propertyKey: string, descriptor: PropertyDescriptor) {descriptor.writable = false;
};function property(target: any, propertyKey: string) {// 修改属性target[propertyKey] = '11223'
}class Person {@propertyname: string;constructor() {}@methodsay() {return 'instance method';}
}const xmz = new Person();// 修改实例方法say
xmz.say = () => {return '5566'   //由于 descriptor.writable = false; 所以此处修改不生效
}
console.log(xmz.name)
console.log(xmz.say())  // nstance method

参数装饰

装饰器参数为:

  • target:当前对象的原型
  • propertyKey:参数的名称
  • index:参数数组中的位置
const init: ParameterDecorator = (...args)=>{console.log(args)
}
class A {constructor() {}getName(@init name: string = 'hello') {console.log(name) // [ {}, 'getName', 0 ]}
}

元数据

import data from './data.json'
import 'reflect-metadata'const Base = (base: string) => {return (target) => {target.prototype.base = base}
}const GetData = (url: string) => {console.log('getData run')const fn: MethodDecorator = (target: any, key, descriptor: PropertyDescriptor) => {const prop = Reflect.getMetadata('key', target)console.log(prop, 'prop')descriptor.value(prop ? data[prop] : data)}return fn;
}const Result = () => {console.log('result run')const fn: ParameterDecorator = (target: any, key, index) => {Reflect.defineMetadata('key', 'data', target)}return fn
}@Base('/api')
class Http {fileName: stringconstructor(name) {this.fileName = name}@GetData('json')getList(@Result() data: any) {console.log(data)}
}
const http = new Http('orange')
http://www.dinnco.com/news/66163.html

相关文章:

  • 营销型品牌网站建设aso优化工具
  • 保定做网站外链吧官网
  • 重新安wordpress网站河南seo排名
  • 创建网页的代码优化官网咨询
  • 公司网站建设备选方案评价标准成都优化网站哪家公司好
  • 肇庆软件建网站公司百度搜索优化软件
  • python 做的网站有哪些合肥网络优化推广公司
  • 商务网站管理与建设青岛网站设计微动力
  • 汨罗哪里有网站开发的公司电话百度指数1000搜索量有多少
  • 中国正规的加盟网站文件外链
  • 精品课程建设网站清单如何增加网站的外链
  • PHP做的哪些大型网站色盲色弱测试
  • 做推文封面图网站百家号关键词排名优化
  • 办公室装修设计平台重庆网站搜索引擎seo
  • 顶级网站建设国外免费发产品的b2b平台
  • 高仿酒网站怎么做客户关系管理
  • 哈尔滨松北区建设局网站发稿网
  • 网站设计的主要步骤查关键词
  • 九江网站设计公司营销咨询公司排名前十
  • 国外的一些网站新手做电商怎么起步
  • 小区安全网站开发论文seo推广专员
  • 济南著名网站建设自助建站网站哪个好
  • 佛山网站建设哪个好点外链交易平台
  • 昵图网免费素材广东seo外包服务
  • 腾讯企业邮箱网页版登录入口seo外包公司报价
  • 各类手机网站建设株洲seo优化推荐
  • 教做世界美食的网站谷歌seo运营
  • 网站建设找哪个平台企业网站怎么注册
  • 怎么做快三彩票网站关键词是什么
  • 南阳网站制作容易被百度收录的网站