[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 ...
随机推荐
- Swift -- SnapKit
Snapkit SnapKit是专门为Swift语言提供AutoLayout布局使用的,特点为语法简洁易用. let subview = UIView() subview.backgroundColo ...
- iOS9 升级设置
今天升级了iOS9, Xcode7.1 ; 打开之前的工程发现网络请求出错了, 参照UM开发文档, 对info.plist进行了配置如下: 1. 以iOS9 SDK编译的工程会默认以SSL安全协议进行 ...
- MAVEN:::::: maven-dependency-plugin (goals "copy-dependencies", "unpack") is not supported
zhuan:http://elan1986.iteye.com/blog/1537967 <!--add by wangquanjun 20140529--> < ...
- python多线程threading.Lock锁用法实例
本文实例讲述了python多线程threading.Lock锁的用法实例,分享给大家供大家参考.具体分析如下: python的锁可以独立提取出来 mutex = threading.Lock() #锁 ...
- 【LA3523】 Knights of the Round Table (点双连通分量+染色问题?)
Being a knight is a very attractive career: searching for the Holy Grail, saving damsels in distress ...
- block的是发送信号的线程,又不是处理槽函数的线程
请问UI线程给子线程发信号,应该用哪种连接方式? 如果子线程正在执行一个函数,我发射信号去执行子线程的另一个函数,那么此时子线程到底会执行什么呢? 用信号量做的同步.第一把信号槽的事件丢到线程的事件队 ...
- Earth to developers: Grow up!
这是篇老外写的文章,主题是针对网络上的一些宗教式的争论,作者叙述了他自己的一些观点.主要从以下6点做了陈述.为了表达的精确性,就直接用英文. 1. Reject dogmatic thinking a ...
- IT传道解惑:心累了就读读
写在开始 学习不是因为缺少时间而是缺少努力 Studies this matter, lacks the time, but is lacks diligently. 只要你想学好,用心去学,肯下功夫 ...
- 【HDOJ】4553 约会安排
线段树.线段树的细节很重要,小数据遍历可以发现问题. /* 4553 */ #include <iostream> #include <string> #include < ...
- 利用if else 求房贷
static void Main(string[] args) { while (true) //主要就是公式的运用和if else 的嵌套 ...