Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can anyone explain what es7 reflect-metadata is all about?

Been studying ES6, JSPM & angular2 for a week now and I found this repo ES6-loader

if we look at the index.html at the bottom script you'll see

 System.import('reflect-metadata')   .then(function() {     return System.import('app/index');   })   .catch(console.log.bind(console)); 

This is using JSPM's systemjs polyfill to get ES6's import.

Question: What is the reflect-metadata really do in this situation? npm reflect-meta The more I read the documentation, the less I understand what it does?

like image 483
Armeen Harwood Avatar asked May 29 '15 21:05

Armeen Harwood


People also ask

What is reflect metadata?

reflect-metadata Allows you to do runtime reflection on types. The native (non reflect-metadata) version of type inference is much poorer than reflect-metadata and consists only of typeof and instanceof .

What is reflect metadata angular?

'reflect-metadata' is a package that is a proposal for ES7. It allow for meta data to be included to a class or function; essentially it is syntax sugar. Example. Angular 2 ES6: @Component({selector: "thingy"}) @View({template: "<div><h1>Hello everyone</h1></div>"}) class Thingy{};


1 Answers

'reflect-metadata' is a package that is a proposal for ES7. It allow for meta data to be included to a class or function; essentially it is syntax sugar.

Example. Angular 2 ES6:

@Component({selector: "thingy"}) @View({template: "<div><h1>Hello everyone</h1></div>"}) class Thingy{}; 

As you can see there are no semicolons after @Component and @View. This is because they are essentially Chains of (meta)data on the class.

Now lets look at that same code in Angular 2 but in ES5:

function Thingy(){} Thingy.annotations = [     new angular.ComponentAnnotation({         selector: "thingy"     }),     new angular.ViewAnnotation({          template: "<div><h1>Hello everyone</h1></div>"     }) ]; 

As you can see the @ symbol abstracts out alot of the annotations property of a class and makes it More D.R.Y.

Taking this one step further Angular team knows that annotations are a bit abstract for the new user. Moreover, the ES5 is just too verbose. which is why they made a.js which will make interfacing with annotations better:

Video to understand this

like image 75
Armeen Harwood Avatar answered Sep 18 '22 19:09

Armeen Harwood