What is the purpose of the keyword "export"?

Asked

Viewed 146 times

7

I created a angular design to start my learning and I am currently using Typescript to do the App. However, a question arose regarding the keyword export (if it really is a keyword).

I used it to create a class and a array.

Class:

export class Anime {
    id: number;
    titulo: string;
    imagem: string;
}

Array:

export const ANIMES: Anime[] = [
    { id: 1, titulo: 'Tate no Yuusha no Nariagari', imagem: 'https://myanimelist.cdn-dena.com/images/anime/1068/97169.jpg' },
    { id: 2, titulo: 'Yakusoku no Neverland', imagem: 'https://myanimelist.cdn-dena.com/images/anime/1125/96929.jpg' },
    { id: 2, titulo: 'Youjo Senki', imagem: 'https://myanimelist.cdn-dena.com/images/anime/5/82890.jpg' }
];

Note that this word is used on two occasions. Therefore, this is where my doubts arise.

Doubts

  1. What is the purpose of the keyword export?
  2. Why use it to create arrays or classes?
  3. She has some relation to Angular itself?

1 answer

8


What is the purpose of the keyword export?

Knows public of other languages? That’s basically it. For some reason Javascript chose to use this, and Typescript adopted it, before JS made it official, to determine that that member can be accessed outside that module, so any member marked that way can be imported into its consumer code, the others cannot, are visible only inside the module itself. We’re just talking about module members, not classes. Remembering that if you are outside a module the file ends up working as a module itself that will be considered exported.

There is the public also that it is only an access control of class members, only that the standard is public and it ends up being optional ( id, titulo and imagem are already public. They ended up separating these things into two keywords, different from other languages.

Why use it to create arrays or classes?

In fact it can be used in any statement of members of a module, has nothing special for these purposes. Your examples only say that the class can be accessed, therefore instantiated, by every application, just like this array.

She has some relation to Angular itself?

Not.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.