[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 ...
随机推荐
- MySQL中的max_connections和max_user_connections 及 MySQL服务器最大连接数的合理设置
max_connections 是指整个mysql服务器的最大连接数: max_user_connections 是指每个数据库用户的最大连接数,比如:虚拟主机可以用这个参数控制每个虚拟主机用户的数据 ...
- HTML5 push
http://blog.csdn.net/yo548720570/article/details/8599947 http://www.oschina.net/question/82993_63312 ...
- poj1436 Horizontally Visible Segments
这是一个区间更新的题目,先将区间放大两倍,至于为什么要放大可以这样解释,按照从左到右有4个区间,y值是[1,5],[1,2],[3,4],[1,4]如果不放大的话,查询[1,4]区间和前面区间的”可见 ...
- *[hackerrank]Tree Covering
https://www.hackerrank.com/contests/illuminati/challenges/tree-covering 这道题先是在上次交流讨论了一下,然后两位百度的朋友先写完 ...
- [topcoder]CorrectMultiplicationTwo
http://community.topcoder.com/stat?c=problem_statement&pm=11609&rd=14547 http://apps.topcode ...
- 循环冗余校验(CRC)算法入门引导
目录 写给嵌入式程序员的循环冗余校验CRC算法入门引导 前言 从奇偶校验说起 累加和校验 初识 CRC 算法 CRC算法的编程实现 前言 CRC校验(循环冗余校验)是数据通讯中最常采用的校验方式.在嵌 ...
- 可恶的QT隐式共享
这个问题隐藏的很深,一般不容易察觉它造成的问题,而只是享受它提供的好处(节省内存,而且速度更快). 但我发现它现在至少造成两个问题: 1. 把大量的QString放到QMap里,使用完毕后清空QMap ...
- jQuery zTree v3.5 实例3 异步树
最终效果: 点击非叶子节点时,向后台发送请求,获取下级菜单 前台代码如下: <%@ page language="java" contentType="text/h ...
- 【HDOJ】4585 Shaolin
Set可解,Treap也可解.(1) Treap /* */ #include <iostream> #include <string> #include <map> ...
- 【CF】207 Div.1 B.Xenia and Hamming
这题目一看很牛逼,其实非常easy.求求最小公倍数,最大公约数,均摊复杂度其实就是O(n). /* 356B */ #include <iostream> #include <str ...