TypeScript: Week Reflection
TypeScript: Week Reflection
Introduction
Type Script already provide decorators to help developers implement reflection.
If we use the technique decorators, we have to add decorators on the target class during developing.
But is there a way to find members of a specific instance?
Here is a code to reflect an object instance by using JSON functions.
The limitations:
- Must provide an class instance
- Cannot find members which have not be initialed.
- Cannot find the class of a member
- Only can provide typeof and isArray attributes.
Source Code
- Source
export class ReflectMemberInfo {
constructor(public name:string, public type: string, public isArray: boolean) {
}
}
export class WeekReflector {
members: ReflectMemberInfo[] = [];
reflect(obj: any): void {
JSON.stringify(obj, (key, value) => {
if (key == '') {
// it is the root object
return value;
}
var isArray = (value instanceof Array);
this.members.push(new ReflectMemberInfo(key, typeof(value), isArray));
return null;
});
}
}
// Test
class TestObject {
memberBoolean: boolean = false;
memberNumber: number = 1;
memberString: string = "Jack";
// memberSymbol: Symbol = Symbol();
memberUndefined?: string = undefined; // type is undefined
memberNull: string | null = null; // type is object
memberCannotFound: string; // cannot be reflected
memberObject: TestNestedObject = new TestNestedObject();
// type is object
memberStringArray: string[] = ["A", "B", "C"];
// type is object
memberObjectArray: TestNestedObject[] = [new TestNestedObject(), new TestNestedObject()];
// type is function
memberFunction: Function = () => {};
// cannot be reflected
public normalFunc(): any {
return null;
}
}
class TestNestedObject {
nestedNumber: number = 2;
nestedString: string = "Mike";
nestedNull: string | null = null;
nestedUndefined?: string = undefined;
nestedStringArray: string[] = ["A", "B", "C"];
}
var reflector = new WeekReflector();
reflector.reflect(new TestObject());
console.log(reflector.members);
- Result
[ ReflectMemberInfo { name: 'memberBoolean', type: 'boolean', isArray: false },
ReflectMemberInfo { name: 'memberNumber', type: 'number', isArray: false },
ReflectMemberInfo { name: 'memberString', type: 'string', isArray: false },
ReflectMemberInfo { name: 'memberUndefined', type: 'undefined', isArray: false },
ReflectMemberInfo { name: 'memberNull', type: 'object', isArray: false },
ReflectMemberInfo { name: 'memberObject', type: 'object', isArray: false },
ReflectMemberInfo { name: 'memberStringArray', type: 'object', isArray: true },
ReflectMemberInfo { name: 'memberObjectArray', type: 'object', isArray: true },
ReflectMemberInfo { name: 'memberFunction', type: 'function', isArray: false } ]
TypeScript: Week Reflection的更多相关文章
- angular2 学习笔记 (Typescript - Attribute & reflection)
refer : https://www.npmjs.com/package/reflect-metadata refer : https://www.typescriptlang.org/docs/h ...
- angular2 学习笔记 (Typescript - Attribute & reflection & decorator)
更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...
- [TypeScript] Reflection and Decorator Metadata
TypeScript allows you to emit decorator metadata which enables more powerful features through reflec ...
- 用 F# 手写 TypeScript 转 C# 类型绑定生成器
前言 我们经常会遇到这样的事情:有时候我们找到了一个库,但是这个库是用 TypeScript 写的,但是我们想在 C# 调用,于是我们需要设法将原来的 TypeScript 类型声明翻译成 C# 的代 ...
- 【FishFX】花式撩骚,打造TypeScript易用框架。
· 栗子入手 假设有以下foo数组,数组中每个对象都拥有id,name两个属性,现在需要查找id > 0的对象数量. const foo: Array<{ id: number, name ...
- TypeScript: Angular 2 的秘密武器(译)
本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...
- TypeScript为Zepto编写LazyLoad插件
平时项目中使用的全部是jQuery框架,但是对于做webapp来说jQuery太过于庞大,当然你可以选择jQuery 2.*针对移动端的版本. 这里我采用移动端使用率比较多的zepto框架,他跟jqu ...
- TypeScript Vs2013 下提示Can not compile modules unless '--module' flag is provided
VS在开发TypeScript程序时候,如果import了模块有的时候会有如下提示: 这种情况下,只需要对当前TypeScript项目生成设置为AMD规范即可!
- Fresnel Reflection - 菲涅尔反射
[Fresnel Reflection - 菲涅尔反射] “菲涅尔”是一个人的名字,因为他发现了一个有关反射的光学现象,这个现象就用这个人的名字命名了.那么,是什么现象呢? 这就是反射/折射与视点角度 ...
随机推荐
- 第1章 什么是TCP-IP
第1章 什么是TCP-IP 什么是网络 网络是计算机或类似计算机的设备之间通过常用传输介质进行通信的集合.通常情况下,传输介质是绝缘的金属导线, 它用来在计算机之间携带电脉冲,介质也可以是电话线,甚至 ...
- linux系统下的SVN安装
1.直接安装 # sudo apt-get install subversion 2. 创建版本库 # sudo mkdir /home/svn # sudo svnadmin create /hom ...
- hue上配置HA的hdfs文件(注意,HA集群必须这样来配置才能访问hdfs文件系统)
按照正常方式配置,发现无论如何也访问不了hdfs文件系统,因为我们是HA的集群,所以不能按照如下配置 将其改为 除此之外,还需要配置hdfs文件的 接着要去hadoop的目录下启动httpfs.sh ...
- 计算机网络-TCP之三次握手/四次握手
.概念 .特点 .背景知识补充 .三次握手 .四次握手 .其他补充 1.概念 TCP(Transmission Control Protocol,传输控制协议)是 在不可靠的IP层之上实现的可靠的数据 ...
- 解决:HTTP 错误 404.0 - Not Found.您要找的资源已被删除、已更名或暂时不可用。(记录帖)
在IIS 上发布web程序时报错,HTTP 错误 404.0 - Not Found.您要找的资源已被删除.已更名或暂时不可用.在网上找了很多资料依然没有解决,后来问了一下同事,他一分钟就帮我搞定了 ...
- 设计APP时我们该怎么做
不得不承认,手机APP已经渗透到我们的生活中,根据数据统计,人们每天平均有3.9个小时是花费在手机APP的使用上的,可以预见,手机APP正在改变我们的生活.手机APP受到人们的欢迎,很多商家也看到了其 ...
- JavaScript 散集合(HashArray)
散列表和散列映射是一样的,我们已经在本章中介绍了这种数据结构. 在一些编程语言中,还有一种叫作散列集合的实现.散列集合由一个集合构成,但是插入. 移除或获取元素时,使用的是散列函数.我们可以重用本章中 ...
- pdf如何转换为word文档
我们经常会遇到需要将PDF转换为WORD文档,对于我来讲,有些PDF没有目录,看起来非常不方便,于是就特别想转成WORD,然后增加目录,想看某一节内容时,快速查找. 这里我总结了一些方法,后续也会不断 ...
- [HNOI 2015]亚瑟王
Description 小 K 不慎被 LL 邪教洗脑了,洗脑程度深到他甚至想要从亚瑟王邪教中脱坑. 他决定,在脱坑之前,最后再来打一盘亚瑟王.既然是最后一战,就一定要打得漂 亮.众所周知,亚瑟王是一 ...
- [SHOI2017]相逢是问候
Description 信息将你我连结.B君希望以维护一个长度为n的数组,这个数组的下标为从1到n的正整数.一共有m个操作,可以 分为两种:0 l r表示将第l个到第r个数(al,al+1,...,a ...