专栏首页绿巨人专栏TypeScript: Week Reflection

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 } ]

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • TypeScript: Week Reflection

    绿巨人
  • TypeScript中的怪语法

    绿巨人
  • TypeScript中的怪语法

    绿巨人
  • TypeScript: Week Reflection

    绿巨人
  • IBM专家告诉你如何完成Linux 服务器加固与安全验证

    在如今的技术领域中,做一个完全安全的系统是一个不可能实现的目标。正如 FBI 的 Dennis Hughes 所说,“真正安全的计算机是没有连线、锁在一个保险箱...

    小小科
  • React 折腾记 - (6) 基于React 16.6 + Antd 3.10.7封装的一个声明式的查询组件

    根据ctype渲染的控件有Input,Button,Select,DatePicker,Cascader,Radio

    CRPER
  • Chrome Devtools

    (1)document.designMode = ‘on’ (2)打开任何网站,在网址栏输入:可手机端 javascdy.setAttribute(‘conte...

    jinghong
  • 区块链社交im及时通讯系统开发

    一提及社交平台,我们应该最先想到的是现在腾讯的微信,这种社交系统已经占领市场的半壁江山。社交平台多以app的形式出现,当然pc端也有,但随着手机端的用户增多,A...

    v13823115027
  • PHP扩展开发之执行run-tests.php脚本报错

    虽然PHP7.4带来了一个新扩展:PHP FFI,FFI提供了高级语言直接的互相调用,而对于PHP来说,FFI让我们可以方便的调用C语言写的各种库。但是这篇文章...

    沈唁
  • 技术转载——详解Java 类的加载、连接和初始化

    系统可能在第一次使用某个类时加载该类,也可能采用预加载机制来加载某个类。本节将会详细介绍类加载、连接和初始化过程中的每个细节。

    慕容千语

扫码关注云+社区

领取腾讯云代金券