• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

TypeScript: Week Reflection

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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人参与评论

请发表评论

全部评论

专题导读
上一篇:
vue3+vuex4.0+typescript使用详情发布时间:2022-07-18
下一篇:
从JavaScript到TypeScript发布时间:2022-07-18
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap