Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between annotation and decorator?

I am confused when to use term annotation and when to use decorator ?

 @Component({
      selector: 'tabs',
      template: `
      `
    })
    export class Tabs {

    }
like image 380
Sarvesh Yadav Avatar asked May 19 '16 08:05

Sarvesh Yadav


People also ask

What are annotations in angular 2?

Annotations are used to create annotations attribute to store the array and pass the metadata to the constructor of the annotated class. Annotations are hard-coded and are used by the Traceur compiler.

What is decorator in Angular?

Decorators are a design pattern that is used to separate modification or decoration of a class without modifying the original source code. In AngularJS, decorators are functions that allow a service, directive or filter to be modified prior to its usage.

What are annotations or metadata?

The rows or tuples are the data, and the columns would be the metadata; they define what each value is for any given row. Annotations are code snippits you put in at the java class or method level that further define data about the given code, but without changing any of the actual coded logic.

What does annotation mean in Java?

Java annotations are metadata (data about data) for our program source code. They provide additional information about the program to the compiler but are not part of the program itself. These annotations do not affect the execution of the compiled program. Annotations start with @ .


1 Answers

A decorator corresponds to a function that is called on the class whereas annotations are "only" metadata set on the class using the Reflect Metadata library.

With TypeScript and ES7, @Something is a decorator. In the context of Angular2, decorators like @Component, @Injectable, ... define metadata for the decorated element using the Reflect.defineMetadata method.

This question could interest you to find out what a decorator actually is:

  • How are decorators (annotations) compiled in Typescript?
like image 153
Thierry Templier Avatar answered Oct 04 '22 13:10

Thierry Templier