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的更多相关文章

  1. angular2 学习笔记 (Typescript - Attribute & reflection)

    refer : https://www.npmjs.com/package/reflect-metadata refer : https://www.typescriptlang.org/docs/h ...

  2. angular2 学习笔记 (Typescript - Attribute & reflection & decorator)

    更新 : 2018-11-27 { date: Date } 之前好像搞错了,这个是可以用 design:type 拿到的 { date: Date | null } 任何类型一但配上了 | 就 de ...

  3. [TypeScript] Reflection and Decorator Metadata

    TypeScript allows you to emit decorator metadata which enables more powerful features through reflec ...

  4. 用 F# 手写 TypeScript 转 C# 类型绑定生成器

    前言 我们经常会遇到这样的事情:有时候我们找到了一个库,但是这个库是用 TypeScript 写的,但是我们想在 C# 调用,于是我们需要设法将原来的 TypeScript 类型声明翻译成 C# 的代 ...

  5. 【FishFX】花式撩骚,打造TypeScript易用框架。

    · 栗子入手 假设有以下foo数组,数组中每个对象都拥有id,name两个属性,现在需要查找id > 0的对象数量. const foo: Array<{ id: number, name ...

  6. TypeScript: Angular 2 的秘密武器(译)

    本文整理自Dan Wahlin在ng-conf上的talk.原视频地址: https://www.youtube.com/watch?v=e3djIqAGqZo 开场白 开场白主要分为三部分: 感谢了 ...

  7. TypeScript为Zepto编写LazyLoad插件

    平时项目中使用的全部是jQuery框架,但是对于做webapp来说jQuery太过于庞大,当然你可以选择jQuery 2.*针对移动端的版本. 这里我采用移动端使用率比较多的zepto框架,他跟jqu ...

  8. TypeScript Vs2013 下提示Can not compile modules unless '--module' flag is provided

    VS在开发TypeScript程序时候,如果import了模块有的时候会有如下提示: 这种情况下,只需要对当前TypeScript项目生成设置为AMD规范即可!

  9. Fresnel Reflection - 菲涅尔反射

    [Fresnel Reflection - 菲涅尔反射] “菲涅尔”是一个人的名字,因为他发现了一个有关反射的光学现象,这个现象就用这个人的名字命名了.那么,是什么现象呢? 这就是反射/折射与视点角度 ...

随机推荐

  1. 新概念英语(1-129)Seventy miles an hour

    Lesson 129 Seventy miles an hour 时速70英里 Listen to the tape then answer this question. What does Ann ...

  2. django的models模块查询方法

    假定models中有一个类BookInfo 模块查询不同于sql语句,模块查询的结果会返回符合条件的整个一行的对象,或者多个对象组成的查询集. 查询集类似列表,有相似的方法. 1 model查询语句: ...

  3. [机器学习实战]K-近邻算法

    1. K-近邻算法概述(k-Nearest Neighbor,KNN) K-近邻算法采用测量不同的特征值之间的距离方法进行分类.该方法的思路是:如果一个样本在特征空间中的k个最相似(即特征空间中最邻近 ...

  4. Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布

    Excel和Word 简易工具类,JEasyPoi 2.1.5 版本发布 摘要: jeasypoi 功能如同名字easy,主打的功能就是容易,让一个没见接触过poi的人员 就可以方便的写出Excel导 ...

  5. 用js来实现那些数据结构05(栈02-栈的应用)

    上一篇文章我们一起实现了栈,那么这一篇文章我们一起来用栈解决问题.看看如何用栈来解决进制转换,平衡圆括号以及汉诺塔问题,使我们对栈有更为深入的理解. 1.进制转换 我们先来看看十进制如何转换成二进制, ...

  6. Swagger+Asp.net WebApi实例

    第一步新建WebApi项目 文件-新建-项目,弹出以下页面 第二步,新建参数项目 第三步 1.自定义输入参数 2.定义公用输出参数 3.定义输出参数 4.定义返回模型 第四步,在webapi项目中新增 ...

  7. Goldwell平台官网简介-欢迎咨询经理罗琪

    Goldwell平台官网简介-欢迎咨询经理罗琪1: Goldwell官网是一家国际衍生品的经纪公司.它对柬埔寨金融市场和客户绝对的承诺,在SECC的监管和保护下,提供了更加多元化的金融交易工具. Go ...

  8. VueJs 源码解析 (四) initRender.Js

    vueJs 源码解析 (四) initRender.Js 在之前的文章中提到了 vuejs 源码中的 架构部分,以及 谈论到了 vue 源码三要素 vm.compiler.watcher 这三要素,那 ...

  9. OpenGL平面阴影

    几种绘制阴影的方法 在OpenGL中,比较常见的绘制阴影的方法有:shadow mapping,shadow volumes以及一种在红宝书上提及的适合在确定平面上绘制阴影的方法. 平面阴影 在确定的 ...

  10. 基于线程池的多线程售票demo2.0(原创)

    继上回基于线程池的多线程售票demo,具体链接: http://www.cnblogs.com/xifenglou/p/8807323.html以上算是单机版的实现,特别使用了redis 实现分布式锁 ...