Last active
August 29, 2015 14:16
-
-
Save iamssen/1a2e3a2344986770673f to your computer and use it in GitHub Desktop.
Typescript 1.5 Annotation 분석
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // @param c typeof Class : target class | |
| // @param annotation typeof any : some annotation object | |
| // | |
| // c.annotations = [annotation] | |
| // | |
| // Class에 annotations 배열을 만들고 annotation을 기록해준다 | |
| function addAnnotation(c: any, annotation: any): any { | |
| (c.annotations || (c.annotations = [])).push(annotation) | |
| return c | |
| } | |
| // annotation object | |
| class AnnotationClass { | |
| a: string | |
| b: number | |
| constructor(a:string, b:number) { | |
| this.a = a | |
| this.b = b | |
| } | |
| } | |
| // 실제 Annotation은 Class 형식이 아니라 | |
| // `function(c:Class) { return c }` 의 decorator function 형태를 가진다 | |
| function Annotation(arg: { a: string, b: number }) { | |
| // return function (c) { | |
| // addAnnotation(c, new AnnotationClass(a, b)) | |
| // } | |
| return c => addAnnotation(c, new AnnotationClass(arg.a, arg.b)) | |
| } | |
| // AtScript 스펙에 있던 | |
| // Class.properties | |
| // Class.annotate | |
| // Class.parameters | |
| // 등은 삭제가 된듯 하다. (소스에 반영이 안된다) | |
| // 현재 annotations 이외의 모든 스펙을 확인할 수 없다 | |
| // 스펙이 어느 정도 축소되었음을 알 수 있다 (아마도 ES5, ES6, Typescript, Dart 모두를 지원하기 위해?) | |
| class ParamClass { | |
| } | |
| // 작성된 Annotation function은 이와 같이 사용할 수 있다 | |
| @Annotation({a: 'class a', b: 123}) | |
| class Sample { | |
| constructor(param:ParamClass) { | |
| } | |
| @Annotation({a: 'func a', b: 234}) | |
| fun1(): string { | |
| return 'hello' | |
| } | |
| } | |
| // [ { a: 'class a', b: 123 } ] | |
| console.log(Sample['annotations']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| node tsc/tsc.js -m commonjs -t es5 ssen.ts ;and node ssen.js |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var __decorate = this.__decorate || function (t, p, a, d) { | |
| d = d || Object.getOwnPropertyDescriptor(t, p); | |
| for (var i = a.length - 1; i >= 0; i--) d = (void 0, a[i])(t, p, d) || d; | |
| d && Object.defineProperty(t, p, d); | |
| }; | |
| // @param c typeof Class : target class | |
| // @param annotation typeof any : some annotation object | |
| // | |
| // c.annotations = [annotation] | |
| // | |
| // Class에 annotations 배열을 만들고 annotation을 기록해준다 | |
| function addAnnotation(c, annotation) { | |
| (c.annotations || (c.annotations = [])).push(annotation); | |
| return c; | |
| } | |
| // annotation object | |
| var AnnotationClass = (function () { | |
| function AnnotationClass(a, b) { | |
| this.a = a; | |
| this.b = b; | |
| } | |
| return AnnotationClass; | |
| })(); | |
| // 실제 Annotation은 Class 형식이 아니라 | |
| // `function(c:Class) { return c }` 의 decorator function 형태를 가진다 | |
| function Annotation(arg) { | |
| // return function (c) { | |
| // addAnnotation(c, new AnnotationClass(a, b)) | |
| // } | |
| return function (c) { return addAnnotation(c, new AnnotationClass(arg.a, arg.b)); }; | |
| } | |
| // AtScript 스펙에 있던 | |
| // Class.properties | |
| // Class.annotate | |
| // Class.parameters | |
| // 등은 삭제가 된듯 하다. (소스에 반영이 안된다) | |
| // 현재 annotations 이외의 모든 스펙을 확인할 수 없다 | |
| // 스펙이 어느 정도 축소되었음을 알 수 있다 (아마도 ES5, ES6, Typescript, Dart 모두를 지원하기 위해?) | |
| var ParamClass = (function () { | |
| function ParamClass() { | |
| } | |
| return ParamClass; | |
| })(); | |
| // 작성된 Annotation function은 이와 같이 사용할 수 있다 | |
| var Sample = (function () { | |
| function Sample(param) { | |
| } | |
| Sample.prototype.fun1 = function () { | |
| return 'hello'; | |
| }; | |
| __decorate(Sample.prototype, "fun1", [Annotation({ a: 'func a', b: 234 })]); | |
| Sample = Annotation({ a: 'class a', b: 123 })(Sample) || Sample; | |
| return Sample; | |
| })(); | |
| // [ { a: 'class a', b: 123 } ] | |
| console.log(Sample['annotations']); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment