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的更多相关文章

  1. [TypeScript] Understanding Generics with RxJS

    Libraries such as RxJS use generics heavily in their definition files to describe how types flow thr ...

  2. springcloud starter(一)

    Spring Cloud - Getting Started Example, 转载自:https://www.logicbig.com/tutorials/spring-framework/spri ...

  3. 浅入浅出Typescript Decorators

    临时起的兴趣,想写一篇关于ts decorator的文章,就花小半天整理了一下...  这东西,在ES2017里好像也有... 文档的话看这里. 因为临时,就没想写太多文字介绍,带少许文字说明直接开撸 ...

  4. TypeScript学习笔记(九):装饰器(Decorators)

    装饰器简介 装饰器(Decorators)为我们在类的声明及成员上通过元编程语法添加标注提供了一种方式. 需要注意的是:装饰器是一项实验性特性,在未来的版本中可能会发生改变. 若要启用实验性的装饰器特 ...

  5. [TypeScript] Dynamically initialize class properties using TypeScript decorators

    Decorators are a powerful feature of TypeScript that allow for efficient and readable abstractions w ...

  6. [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 ...

  7. TypeScript装饰器(decorators)

    装饰器是一种特殊类型的声明,它能够被附加到类声明,方法, 访问符,属性或参数上,可以修改类的行为. 装饰器使用 @expression这种形式,expression求值后必须为一个函数,它会在运行时被 ...

  8. [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 ...

  9. [Typescript] Build Method decorators in Typescript

    To using decorate, we can modifiy tsconfig.json: { "compilerOptions": { ... "experime ...

随机推荐

  1. OC - 正则表达式 - RegexKitLite

    正则表达式使用步骤: 1. 创建正则表达式对象, 设置约束条件; NSString *pattern = @"\\d{1,3}"; NSRegularExpression *reg ...

  2. C#中的委托事件的分析

    推荐:http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html 委托和事件在 .NET Framework 中的应用非常广泛,然而, ...

  3. sqlite的源代码加密,以及其它一些文章

    一.       给数据库加密 前面所说的内容网上已经有很多资料,虽然比较零散,但是花点时间也还是可以找到的.现在要说的这个——数据库加密,资料就很难找.也可能是我操作水平不够,找不到对应资料.但不管 ...

  4. VC自动与Internet时间服务器同步更新

    在VCKBASE.CSDN里挖了许久的坟,才找到一些有点用的资料,最后自己整理出这样的个函数,方面VC实现时间同步,多的不说,自己看源码,根据自己的需要可以适当修改源码: #include <W ...

  5. WebService调用http://www.webxml.com.cn中的一些服务示例

    1.打开http://www.webxml.com.cn网站下的“全部WebService”,会看到如下图的图片 2.点击打开WSDL连接,如下图,并保存为一个后缀名为wsdl文件,放在项目的根目录下 ...

  6. Adobe Flash Builder 4.7下载地址及破解补丁(32位&64位)

    Adobe FlashBuilder 4.7是开发flex的利器,能显著提高flex的开发效率.最新版的是4.7,去官网上下载时每次都要登录才能下载,特麻烦,这次下载时就把相关的下载地址给记录了下来, ...

  7. 查看本机上的端口使用情况netstat -an

    1.查找本机上的端口使用情况 netstat -an 2.查找指定端口的使用情况 C:\Windows\System32>netstat -ano | find "8002" ...

  8. ELK之nginx日志分析图表创建

    一.kibana面板介绍 Discover:查询数据Visualize:统计图表Dashboard:显示面板,添加相应的图表在面板中Settings:创建索引 二.图表创建 1.饼图创建 以创建一个状 ...

  9. Android Http Server

    Android Http Server 1 引言          Android如何构建Http服务器呢?本文的小例子,约莫着,还是能做个参考的^^.恩,例子实现的是PC浏览手机文件,支持了下载和删 ...

  10. 字符串(AC自动机):HDU 5129 Yong Zheng's Death

    Yong Zheng's Death Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 512000/512000 K (Java/O ...