[TypeScript] Understanding Decorators
Decorators are a feature of TypeScript that are becoming more and more common in many major libraries. This lesson walks you through what decorators are and how to create your own.
Nomarl way to decorate a object :
const Person = {name: 'John'};
function addAge(age){
return function(person){
return {
age,
name: person.name
}
}
}
const john = addAge(30)(Person);
console.log(john); // {name: "John", age: 30}
In Typescript, we can enable "experimentaDecorators", which is ES7 feature:
{
"compilerOptions": {
"rootDir": "src",
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": false,
"outDir": "./dist",
"noEmitOnError": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
function addAge(age){
return function(targetClass){
return class{
age = age;
name = new targetClass().name;
}
}
}
@addAge(30)
class Person{
name = "Johnn";
}
const john = new Person();
console.log(john); // {name: "Johnn", age: 30}
As before we create addAge function as decorator, different from before, it return class, because we want to decorate Person class.
After the decorator, we will have age prop on the person class.
[TypeScript] Understanding Decorators的更多相关文章
- [TypeScript] Understanding Generics with RxJS
Libraries such as RxJS use generics heavily in their definition files to describe how types flow thr ...
- springcloud starter(一)
Spring Cloud - Getting Started Example, 转载自:https://www.logicbig.com/tutorials/spring-framework/spri ...
- 浅入浅出Typescript Decorators
临时起的兴趣,想写一篇关于ts decorator的文章,就花小半天整理了一下... 这东西,在ES2017里好像也有... 文档的话看这里. 因为临时,就没想写太多文字介绍,带少许文字说明直接开撸 ...
- TypeScript学习笔记(九):装饰器(Decorators)
装饰器简介 装饰器(Decorators)为我们在类的声明及成员上通过元编程语法添加标注提供了一种方式. 需要注意的是:装饰器是一项实验性特性,在未来的版本中可能会发生改变. 若要启用实验性的装饰器特 ...
- [TypeScript] Dynamically initialize class properties using TypeScript decorators
Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions w ...
- [Vue + TS] Create your own Decorators in Vue with TypeScript
We’ve used @Watch, @Inject and more decorators from vue-property-decorator. In this lesson however w ...
- TypeScript装饰器(decorators)
装饰器是一种特殊类型的声明,它能够被附加到类声明,方法, 访问符,属性或参数上,可以修改类的行为. 装饰器使用 @expression这种形式,expression求值后必须为一个函数,它会在运行时被 ...
- [Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript
Vue 2.2 introduced a simple dependency injection system, allowing you to use provide and inject in y ...
- [Typescript] Build Method decorators in Typescript
To using decorate, we can modifiy tsconfig.json: { "compilerOptions": { ... "experime ...
随机推荐
- Pentaho Data Integration (三) Pan
官网连接: http://wiki.pentaho.com/display/EAI/Pan+User+Documentation Pan Pan 是一个可以执行使用Spoon编辑的transforma ...
- Discuz! 7.2 SQL注入exp
已经有人写出一些工具了,但是感觉不怎么好用,就自己写了个. 参数:1.可直接getshell2.爆管理账号密码3.爆表前缀如果表前缀不是默认的cdb_ 只需更改代码中的 $table即可,方便快捷. ...
- VCC,VDD,VEE,VSS,VPP 表示的意义
转自VCC,VDD,VEE,VSS,VPP 表示的意义 VCC,VDD,VEE,VSS,VPP 表示的意义 版本一: 简单说来,可以这样理解: 一.解释 VCC:C=circuit 表示电路的意思, ...
- 打造属于自己的Altium Designer 3D封装库,不需要懂专门的三维设计软件
看到Andy_2020发的帖子“Altium Designer专题”之后,对Altium Designer的3D功能很感兴趣,着手自己做一个AD的3D封装库.刚开始按照Andy介绍的方法,学了两天So ...
- 程序异常捕获库 - CrashRpt
CrashRpt.dll用来在应用程序出现异常crash时,捕获到错误. 并收集出错信息: MiniDump文件.硬件信息.系统信息.出错信息.进程信息.服务信息.驱动信息.启动信息.软件列表.端口信 ...
- message 匹配不上grok正则 也会写入到elasticsearch
{ "message" => "scan test 20161201", "@version" => "1" ...
- default 关键字泛型代码中的默认关键字(C# 编程指南)
在泛型类和泛型方法中产生的一个问题是,在预先未知以下情况时,如何将默认值分配给参数化类型 T: T 是引用类型还是值类型. 如果 T 为值类型,则它是数值还是结构. 给定参数化类型 T 的一个变量 t ...
- win8.1右键没有“新建”选项
# 记事本中新建以下代码,复制粘贴进去,# 将.txt格式改为.reg,双击运行它就行了. Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROO ...
- bzoj3876
不高兴的回忆啊啊啊当初这种简单题因为自己作死就暴零0了这题在OJ上是简单的最小有下界费用流,增广到正费用为止因为算的是总时限但实际的话似乎要用pacman吃豆豆那题的方法先用dp跑出第一次的增广路再用 ...
- wpa_supplicant 配置与应用
概述 wpa_supplicant是wifi客户端(client)加密认证工具,和iwconfig不同,wpa_supplicant支持wep.wpa.wpa2等完整的加密认证,而iwconfig只能 ...