[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 ...
随机推荐
- 贴板子系列_1-km算法,匈牙利算法
KM算法 #include <bits/stdc++.h> #define N 1500 #define inf 999999999 using namespace std; ,ny=,k ...
- PHP程序的一次重构记录
项目和新需求: 我们有一个PHP写的webmail系统,有一个mail_list.php用于展现用户的邮件列表这个页面支持folderId参数(因为邮件是存在不同的文件夹下的)由于邮件太多所以支持翻页 ...
- Cloud Insight!StatsD 系监控产品新宠!
年关将至,Cloud Insight 正式版悄然上线了.没有大张旗鼓的宣传,也没有热热闹闹的庆祝,只是一群人在上线前踏踏实实的优化了两周,然后发版,就是这样一件简单的事. 然而就是这样一个低调的产品, ...
- C++控制台程序中使用定时器
转自博客:http://www.cnblogs.com/phinecos/archive/2008/03/08/1096691.html 作者:洞庭散人 “我现在项目是一个控制台程序,用到的Win32 ...
- ANDROID_MARS学习笔记_S03_008_GOOGLEMAP2
一.简介 二.代码1.xml(1)main.xml <?xml version="1.0" encoding="utf-8"?> <Linea ...
- android利用剪切板来实现数据的传递
在Android开发中我们经常要遇到的一个问题就是数据在不同的Activity之间的共享.在Android开发中有很多种方法可以达到这个目地. 这里介绍一种比较常见.又常用的一种方法就是使用剪切板.我 ...
- windows桌面添加右键环境
1.组合键win + R,输入regedit,回车 打开注册表编辑器 2.找到目录中[HKEY_CLASSES_ROOT\Directory\Background\shell]对其右键,新建一个项 ...
- oracle命中率模型计算
命中率模型是在owi之前比较常用的一种诊断性能问题的方法,通过命中率的计算,发现系统中的一些设置是否合理,当命中率不高的时候,通过调整一些参数和设置,提高命中率,有效的提高系统的性能和吞吐量.但当系统 ...
- MySQL源码之mysqld启动
启动mysqld,并进入listen阶段 函数调用栈: mysqld_main(): my_init();初始化变量,锁,错误串 my_thread_global_init ...
- 清理ms sql server 大日志文件数据
1.手动分离数据库: 2.手动删除日志文件: 3.重新生成日志文件: CREATE DATABASE FMIS0 ON (FILENAME = 'E:\FMIS0_DATA\FMIS0-Date') ...