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. Java 文件操作大全

    Java 文件操作大全 //1.创建文件夹 //import java.io.*; File myFolderPath = new File(str1); try { if (!myFolderPat ...

  2. ipad ------ 与iPhone的差别

    1. 差异 iPhone是手机,iPad.iPad Mini是平板电脑 iPhone和iPad开发的区别 屏幕的尺寸 \分辨率 UI元素的排布 \设计 键盘 API 屏幕方向的支持 … … 2. iP ...

  3. iOS 一些struct类型的NSLog输出格式-b

    我们经常会输出一些坐标尺寸信息之类的,比如view的frame,是CGRect类型的,用frame.oringial.x 和frame.size.width来做NSLog参数好麻烦,还好苹果对这些常用 ...

  4. Eclipse导入Android项目的正确方法

    转自Eclipse导入Android项目的正确方法 看网上流传的Eclipse导入项目的方法都是在新建Android程序时使用"Create project form existing so ...

  5. Java使用JAX-WS来写webservice时 Unable to create JAXBContext

    webservice,作为web开发人员来说必须掌握的一门技术,它的好处这里就不多说了,eclipse中自带了一种生成webservice的 方法,使用JAX-WS,如果我没有弄错的话,它需要java ...

  6. 有关ARM大小端及网络字节序

    http://blog.sina.com.cn/s/blog_62b250b50101ntjs.html

  7. Android ViewDragHelper源码解析

    在自定义ViewGroup的过程中,如果涉及到View的拖动滑动,ViewDragHelper的使用应该是少不了的,它提供了一系列用于用户拖动子View的辅助方法和相关的状态记录,像Navigatio ...

  8. c#4.0新特性之协变与逆变

    1.C#3.0以前的协变与逆变 如果你是第一次听说这个两个词,别担心,他们其实很常见.C#4.0中的协变与逆变[1](Covariance and contravariance)有了进一步的完善,主要 ...

  9. 水题:HDU 5119 Happy Matt Friends

    Matt has N friends. They are playing a game together.Each of Matt's friends has a magic number. In t ...

  10. HDU1394-Minimum Inversion Number

    http://acm.hdu.edu.cn/showproblem.php?pid=1394 Minimum Inversion Number Time Limit: 2000/1000 MS (Ja ...