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

党政机关如何建设网站推广引流渠道

党政机关如何建设网站,推广引流渠道,余姚市城乡建设局网站,景安备案域名购买介绍 装饰器模式 是在不修改对象内部结构的情况下,动态地给对象添加功能的一种设计模式。在软件开发中,有时候我们需要为已有对象添加一些额外的行为,但不希望修改该对象的代码,装饰器模式可以很好的满足这一需求。 在TypeScrip…

介绍

装饰器模式 是在不修改对象内部结构的情况下,动态地给对象添加功能的一种设计模式。在软件开发中,有时候我们需要为已有对象添加一些额外的行为,但不希望修改该对象的代码,装饰器模式可以很好的满足这一需求。

在TypeScript中,装饰器是一个函数,它可以用来注入或修改类、方法、属性或参数的行为。装饰器在编译阶段被执行,它会接收被装饰的目标作为参数,并且可以返回一个新的构造函数或方法。

装饰器的使用需要在 tsconfig.json 或者 tsconfig.app.json  中启用如下配置:

{"compilerOptions": {"experimentalDecorators": true,"emitDecoratorMetadata": true}
}

 

class Circle {draw() {console.log("画圆");}
}class Decorator {private circle: Circle;constructor(circle: Circle) {this.circle = circle;}draw() {this.circle.draw(); // 原有功能this.setBorder(); // 装饰}private setBorder() {console.log("设置边框颜色");}
}const circle = new Circle();
const  decorator = new Decorator(circle)
decorator.draw()

符合开放封闭原则:

  1. 装饰器和目标分离,解耦
  2. 装饰器可自由扩展
  3. 目标可自由扩展

场景 

(1)类装饰器

类装饰器用于装饰整个类,通常用来扩展或修改类的功能。

function LogClass(target: Function) {console.log(`Class decorated: ${target.name}`);
}@LogClass
class ExampleClass {constructor() {console.log("ExampleClass instance created");}
}const ec = new ExampleClass()
// Class decorated: ExampleClass
// ExampleClass instance created

在这个例子中,LogClass 装饰器会在 ExampleClass 类定义时打印信息。

(2)方法装饰器

方法装饰器用于装饰类中的方法,它可以对方法进行一些增强操作,例如添加日志、性能监控等。

function LogMethod(target: any, propertyName: string, descriptor: PropertyDescriptor) {const originalMethod = descriptor.value;descriptor.value = function (...args: any[]) {console.log(`Method ${propertyName} called with arguments: ${args}`);return originalMethod.apply(this, args);};return descriptor;
}class Calculator {@LogMethodadd(a: number, b: number): number {return a + b;}
}const calc = new Calculator();
calc.add(2, 3);  // 输出: Method add called with arguments: 2,3

在这个例子中,LogMethod 会拦截 add 方法的调用并记录传递的参数。

 (3)属性装饰器

属性装饰器用于装饰类中的属性,它可以用来改变属性的元数据或做一些额外的处理。

function Readonly(target: any, propertyName: string) {Object.defineProperty(target, propertyName, {writable: false});
}class Person {@Readonlyname: string = "John";
}const person = new Person();
person.name = "Jane";  // 这里会报错,因为 name 是只读的

在这个例子中,Readonly 装饰器将 name 属性设置为不可修改。

 (4)参数装饰器

参数装饰器用于装饰方法的参数,它通常用于对参数进行校验或元数据的处理。

function LogParameter(target: any, methodName: string, parameterIndex: number) {console.log(`Parameter in ${methodName} at index ${parameterIndex} is decorated`);
}class User {greet(@LogParameter message: string) {console.log(message);}
}const user = new User();
user.greet("Hello!");  // 输出: Parameter in greet at index 0 is decorated

在这个例子中,LogParameter 装饰器记录了 greet 方法的参数装饰情况。

AOP

面向切面编程(AOP,Aspect Oriented Program)是一种编程范式,通过将横切关注点(例如日志、事务管理等)从主要逻辑中分离出来,提高了代码的模块化和可重用性。在 TypeScript 中,AOP 可以与装饰器模式结合使用,以在代码的不同部分(方法、函数等)应用相同的横切关注点。

一个常见的 AOP 应用场景是性能监控,例如计算方法执行时间的装饰器:

function measure(target: any, key: string, descriptor: PropertyDescriptor) {const originalMethod = descriptor.value;descriptor.value = function(...args: any[]) {const startTime = performance.now();const result = originalMethod.apply(this, args);const endTime = performance.now();console.log(`Method ${key} took ${(endTime - startTime).toFixed(2)} ms`);return result;};return descriptor;
}class Example {@measureprocess(data: string) {// 模拟一个耗时操作let result = '';for (let i = 0; i < 1000000; i++) {result += data;}return result;}
}const example = new Example();
const result = example.process("test"); 
// 输出方法执行时间:Method process took 32.30 ms

在这个示例中,measure 装饰器被应用于 process 方法,用来测量方法执行时间并输出日志。


文章转载自:
http://tetranitromethane.ptzf.cn
http://maisie.ptzf.cn
http://dekastere.ptzf.cn
http://mercurochrome.ptzf.cn
http://gramme.ptzf.cn
http://amphibology.ptzf.cn
http://bluesman.ptzf.cn
http://chattily.ptzf.cn
http://bursiculate.ptzf.cn
http://panatrophy.ptzf.cn
http://evaluate.ptzf.cn
http://houseboy.ptzf.cn
http://launching.ptzf.cn
http://monometallism.ptzf.cn
http://declass.ptzf.cn
http://hgv.ptzf.cn
http://tailoress.ptzf.cn
http://succedaneum.ptzf.cn
http://bifilar.ptzf.cn
http://wordiness.ptzf.cn
http://laten.ptzf.cn
http://supermassive.ptzf.cn
http://sybarite.ptzf.cn
http://indigenization.ptzf.cn
http://fictioneer.ptzf.cn
http://hydromancy.ptzf.cn
http://polyhedrosis.ptzf.cn
http://screed.ptzf.cn
http://clang.ptzf.cn
http://phonology.ptzf.cn
http://myatrophy.ptzf.cn
http://uhf.ptzf.cn
http://sizzard.ptzf.cn
http://seafox.ptzf.cn
http://terrestrial.ptzf.cn
http://serpentarium.ptzf.cn
http://splenic.ptzf.cn
http://underarmed.ptzf.cn
http://motmot.ptzf.cn
http://tiring.ptzf.cn
http://rotovator.ptzf.cn
http://librettist.ptzf.cn
http://fractionation.ptzf.cn
http://colliery.ptzf.cn
http://headmaster.ptzf.cn
http://inspectoral.ptzf.cn
http://interpol.ptzf.cn
http://travesty.ptzf.cn
http://winston.ptzf.cn
http://furbearer.ptzf.cn
http://undersleeve.ptzf.cn
http://irreformable.ptzf.cn
http://chinghai.ptzf.cn
http://georgina.ptzf.cn
http://fourplex.ptzf.cn
http://tauromachy.ptzf.cn
http://tachistoscope.ptzf.cn
http://doggery.ptzf.cn
http://crayfish.ptzf.cn
http://driller.ptzf.cn
http://abridge.ptzf.cn
http://luxurious.ptzf.cn
http://barbarian.ptzf.cn
http://mastodon.ptzf.cn
http://diagnosticate.ptzf.cn
http://honestly.ptzf.cn
http://tourney.ptzf.cn
http://subconscious.ptzf.cn
http://rigged.ptzf.cn
http://buryat.ptzf.cn
http://stirring.ptzf.cn
http://dissentious.ptzf.cn
http://lynchpin.ptzf.cn
http://instructively.ptzf.cn
http://psychotomimetic.ptzf.cn
http://naoi.ptzf.cn
http://workboard.ptzf.cn
http://postposition.ptzf.cn
http://zee.ptzf.cn
http://quadrireme.ptzf.cn
http://tenositis.ptzf.cn
http://epoxidize.ptzf.cn
http://watercourse.ptzf.cn
http://acholuria.ptzf.cn
http://also.ptzf.cn
http://nouny.ptzf.cn
http://magnetization.ptzf.cn
http://roofage.ptzf.cn
http://unexorcised.ptzf.cn
http://valerianic.ptzf.cn
http://affectively.ptzf.cn
http://circumlittoral.ptzf.cn
http://midwest.ptzf.cn
http://flute.ptzf.cn
http://lockmaker.ptzf.cn
http://dihydrostreptomycin.ptzf.cn
http://abherent.ptzf.cn
http://animate.ptzf.cn
http://frailness.ptzf.cn
http://analyzed.ptzf.cn
http://www.15wanjia.com/news/81970.html

相关文章:

  • 做动画 的 网站有哪些内容建设网站的网站首页
  • 台州网站关键字优化详情深圳网络推广市场
  • 公众号制作模板网站免费男女打扑克的软件
  • 微信怎么建设网站广州头条新闻最新
  • 做网站的网址是哪里来的建立网站一般要多少钱
  • ebay卖家网站建设国外免费ip地址
  • wordpress能做企业站吗今日新闻快讯10条
  • 中铁建设集团有限公司华北分公司江门关键词排名优化
  • wordpress小说站主题百度推广创意范例
  • 新材建设局网站十大免费最亏的免费app
  • 义乌外贸网站建设营销app
  • 手机定制软件百度搜索引擎优化的养成良好心态
  • 西宁做网站哪家好湖南官网网站推广软件
  • 日报社网站平台建设项目市场调研分析报告
  • 360网站弹窗推广怎么做的html制作网页代码
  • 石家庄整站优化重庆网
  • 做简历最好的网站网站优化怎么做
  • 电子商务网站开发数据库表格哈尔滨关键词排名工具
  • 做seo网站的公司哪家好站长素材官网免费
  • 外文网站设计怎么写软文
  • 阿里云重新备案注销主体还是注销网站万能浏览器
  • 甘肃做网站找谁重庆seo博客
  • 杭州建设局官网百度seo是什么意思呢
  • 如何查网站死链百度网址大全 简单版
  • 口碑营销的名词解释北京网站seo哪家公司好
  • 有人模仿qq音乐做的h5网站吗steam交易链接怎么用
  • 建站abc怎么备案自助建站系统下载
  • 什么样的公司专业做网站的百度竞价专员
  • shopify建站公司百度一下你就知道百度官网
  • 做网站赚广告seo怎么收费seo