[TypeScript] Reflection and Decorator Metadata
TypeScript allows you to emit decorator metadata which enables more powerful features through reflection. This lesson show you how decorators and reflection fit together and how to configure your own decorators to use reflection.
For now, if we look at the compiled file:
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
function addAge(age) {
return function (targetClass) {
return (function () {
function class_1() {
this.age = age;
this.name = new targetClass().name;
}
return class_1;
}());
};
}
var Person = (function () {
function Person() {
this.name = "Johnn";
}
Person = __decorate([
addAge(30)
], Person);
return Person;
}());
var john = new Person();
console.log(john); // {name: "Johnn", age: 30}
It decorates addAge to Person class.
Now if we enable "emitDecoratorMetadata" in tsconfig.json:
{
"compilerOptions": {
"rootDir": "src",
"module": "commonjs",
"target": "es5",
"noImplicitAny": false,
"sourceMap": false,
"outDir": "./dist",
"noEmitOnError": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true
},
"exclude": [
"node_modules",
"typings/main",
"typings/main.d.ts"
]
}
Compile the files again, now we get:
var Person = (function () {
function Person() {
this.name = "Johnn";
}
Person = __decorate([
addAge(30),
__metadata('design:paramtypes', [])
], Person);
return Person;
}());
It also add metadata.
Install:
npm install reflect-metadata crypto --save
Index.html:
<script>
System.config({
packages: {
"dist": {
"defaultExtension": "js",
"main": "main"
},
"rxjs": {
"defaultExtension": "js"
}
},
map: {
"lodash": "https://npmcdn.com/lodash@4.13.1",
"rxjs": "node_modules/rxjs",
"reflect-metadata": "node_modules/reflect-metadata/Reflect.js",
"crypto": "node_modules/crypto/sha1.js",
}
}); System.import("dist")
</script>
main.ts:
import 'reflect-metadata'; function example(){
return function(targetClass){
const types = Reflect.getMetadata('design:paramtypes', targetClass);
console.log(types);
return targetClass
}
} @example()
class Person{
constructor(name: string, age: number){ }
}
new Person("John", 10);
So in the example() fucntion, we console log out types, it will show:
That means we were able to make it generic, so that any class that comes through into this example decorator, we can look up its types and then use those types to modify or pass into the constructor, and return the class decorated however we want.
[TypeScript] Reflection and Decorator Metadata的更多相关文章
- TypeScript 1.5 Beta带来修饰元数据支持
(此文章同时发表在本人微信公众号"dotNET每日精华文章") 今天由于有点小感冒,就不长篇大论了,简单介绍一下和VS 2015 RC一同发布的TypeScript 1.5 Bet ...
- egg.js路由的优雅改造
引言 在使用express,koa, 或者是egg.js进行node server开发的过程中,我们的路由基本上都是定义在controller层的,框架对于 node 原生路由都会进行一层封装,一版都 ...
- google protobuf 使用示例
定义.proto接口文件 package tutorial; message Person { required ; required int32 id = ; //unique ID number ...
- caffe源码学习之Proto数据格式【1】
前言: 由于业务需要,接触caffe已经有接近半年,一直忙着阅读各种论文,重现大大小小的模型. 期间也总结过一些caffe源码学习笔记,断断续续,这次打算系统的记录一下caffe源码学习笔记,巩固一下 ...
- 如何用vue-cli3脚手架搭建一个基于ts的基础脚手架
目录 准备工作 搭建项目 vue 中 ts 语法 项目代理及 webpack 性能优化 其他 忙里偷闲,整理了一下关于如何借助 vue-cli3 搭建 ts + 装饰器 的脚手架,并如何自定义 web ...
- angular2 学习笔记 (Typescript - Attribute & reflection & decorator)
更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...
- angular2 学习笔记 (Typescript - Attribute & reflection)
refer : https://www.npmjs.com/package/reflect-metadata refer : https://www.typescriptlang.org/docs/h ...
- 异常:“System.Reflection.Metadata”已拥有为“System.Collections.Immutable”定义的依赖项
参考动态执行T4模板:https://msdn.microsoft.com/zh-cn/library/bb126579.aspx 我项目是.NET Framework 4.5控制台应用程序写的. 执 ...
- TypeScript: Week Reflection
TypeScript: Week Reflection Introduction Type Script already provide decorators to help developers i ...
随机推荐
- JVM 找出最耗 cpu的线程 并打印线程栈
监控JVM中最占cpu的线程 top -Hp pid JVM中最占cpu的线程ID -o THREAD,tid,time | awk 'BEGIN {count=0; } { if($2>0.3 ...
- JavaScript高级之函数的四种调用形式
主要内容 分析函数的四种调用形式 弄清楚函数中this的意义 明确构造函对象的过程 学会使用上下文调用函数 了解函数的调用过程有助于深入学习与分析JavaScript代码. 本文是JavaScript ...
- 工作总结:文件对话框的分类(C++)
原文地址:http://www.jizhuomi.com/software/173.html 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见到这两种文件对话框.例如 ...
- webkit javascript
http://www.infoq.com/cn/news/2013/02/douglas-interview http://blog.csdn.net/horkychen/article/detail ...
- iOS各种系统通知Name
当了一次搬运工,原地址:http://blog.csdn.net/db905517804/article/details/50569949 用法: - (void) viewWillAppear:(B ...
- 移动应用产品开发-android开发项目记录
今天主要在做app前端开发,做了一个资讯列表和资讯详情,主要模仿网易新闻来做,页面布局简单,但java代码和实现比较麻烦 毕竟才开始做,研究的不那么透彻,也不是很熟练 用心去做,专注开发,今天也算作出 ...
- 【HDOJ】1978 How many ways
DFS. #include <stdio.h> #include <string.h> #define MAXNUM 105 int map[MAXNUM][MAXNUM], ...
- efront二次开发记要
efront系统是一套开源的在线学习系统,是用PHP编写的,内含“考试”功能.该系统的开源的是社区版,虽然看上去功能强大,但使用起来却很不符合国情.为了让公司使用,先做了一次最简化的二次开发,由于是最 ...
- Codevs_1230_元素查找_(set/Hash)
描述 http://codevs.cn/problem/1230/ ... 1230 元素查找 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目 ...
- [DP] LCS小结
额..失误.. LCS是Longest Common Subsequence的缩写,即最长公共子序列.一个序列,如果是两个或多个已知序列的子序列,且是所有子序列中最长的,则为最长公共子序列. DP.O ...