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. 第一次制作和使用图标字体-IcoMoon

    开题:之前就有所耳闻,最近两天第一次运用到图标字体.刚开始嘛,一脸懵逼的状态.成功运用之后就来记录一下使用过程咯! 1. 打开在线生成工具:https://icomoon.io/app/#/selec ...

  2. 解决IE下a标签点击有虚线边框的问题

    解决IE下a标签点击有虚线边框的问题 关键词:IE去除虚线边框.IE解决a标签虚线问题 先看看IE下,a标签出现的虚线边框问题: (上面中,红线包裹的就是一个翻页的按钮,按钮实际是hml的a标签做的, ...

  3. 什么是MQTT协议?

    MQTT协议介绍 MQTT协议是什么? MQTT(Message Queuing Telemetry Transport Protocol)的全称是消息队列遥感传输协议的缩写,是一种基于轻量级代理的发 ...

  4. codeforces 798c Mike And Gcd Problem

    题意: 给出一个数列,现在有一种操作,可以任何一个a[i],用a[i] – a[i+1]和a[i]+a[i+1]替代a[i]和a[i+1]. 问现在需要最少多少次操作,使得整个数列的gcd大于1. 思 ...

  5. React 深入系列3:Props 和 State

    文:徐超,<React进阶之路>作者 授权发布,转载请注明作者及出处 React 深入系列3:Props 和 State React 深入系列,深入讲解了React中的重点概念.特性和模式 ...

  6. [持续开源]基于nodejs+ligerui的一款mongodb web 端查询工具(MongoStudio)

    这是我2015年入猫酷写的一款内部工具,目的是为了开发人员可以查询数据库数据以调查线上bug,数据不仅限业务数据及日志数据,其目的是为了避免开发人员直接链接生产环境数据库,以免误操作影响生产. 当时是 ...

  7. [LeetCode] Increasing Subsequences 递增子序列

    Given an integer array, your task is to find all the different possible increasing subsequences of t ...

  8. 常用linux命令备忘

    备忘: 关闭防火墙:# systemctl stop firewalld 查看防火墙状态:#  systemctl status firewalld 停止防火墙:#  systemctl disabl ...

  9. 用redis的订阅发布解决了扫码支付实时响应的问题

    一.场景描述: PC收银台的浏览器展示了收款二维码,用户扫了支付二维码,支付完成后,浏览器需要实时响应支付结果. 二.问题描述: 扫码支付的支付结果一般通过服务端回调和主动查询来获取,显示二维码之后, ...

  10. Struts2--二次提交

    在Struts2中,使用token的方式来防止二次提交.并且在默认的拦截器栈中提供了两个默认拦截器Token Interceptor和Token Session Interceptor.必须要在for ...