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. 详解Ajax请求(三)——jQuery对Ajax的实现及serialize()函数对于表单域控件参数提交的使用技巧

    原生的Ajax对于异步请求的实现并不好用,特别是不同的浏览器对于Ajax的实现并不完全相同,这就意味着你使用原生的Ajax做异步请求要兼顾浏览器的兼容性问题,对于java程序员来讲这是比较头疼的事情, ...

  2. notepad++运行Python

    1.打开notepad++的菜单栏,点击run 2.输入cmd /k python "$(FULL_CURRENT_PATH)" & PAUSE & EXIT 3. ...

  3. Mac里安装配置Jdk

    #下载jdk7的mac版 #官网下载地址http://www.oracle.com/technetwork/java/javase/downloads/jdk7-downloads-1880260.h ...

  4. shell:bash环境

    1.什么是shell shell一般代表两个层面的意思,一个是命令解释器,比如BASH,另外一个是shell脚本. 命令解释器shell的发展史,sh-csh-ksh-tcsh-bash. 2.命令的 ...

  5. JavaScript是如何面向对象的

    一.引言 在16年的10月份,在校内双选会找前端实习的时候,hr问了一个问题:JavaScript的面向对象理解吗?我张口就说"JavaScript是基于原型的!".然后就没什么好 ...

  6. Text-鼠标点击事件

    from tkinter import * import webbrowser master=Tk() text=Text(master,width=50,height=20) text.pack() ...

  7. AutoFac+MVC+WebApi源码----我踩过的坑

    发现网上关于AutoFac的Demo源码比较少,综合MVC和WepApi的更少.所以贴出源码 WebApi项目(MVC4不需要引用,历史遗留问题,人懒没删) 建项目 新建类库IAutoFacDal(接 ...

  8. 一篇文章说透Nginx的rewrite模块

    rewrite模块即ngx_http_rewrite_module模块,主要功能是改写请求URI,是Nginx默认安装的模块.rewrite模块会根据PCRE正则匹配重写URI,然后发起内部跳转再匹配 ...

  9. 031718-js变量、数据类型、运算符

    1.关键字.标识符.变量(是一个名称,最好用字母开头,对大小写敏感).常量 (是有数据类型的一个值) 变量: ①定义并赋值 ②使用   2.数据类型:数字  字符串  布尔  null  undefi ...

  10. Python系列之 - 上下文管理协议

    with obj as f: '代码块' 1.with obj ---->触发obj.__enter__(),拿到返回值 2.as f----->f=返回值. 3.with obj as ...