user12109818
user12109818

Reputation:

Angular: Difference between Export and Public Class?

In Angular, what is the difference between an export class, and a public class?

"Angular imports/exports are used to make the content of one module available to be used in another module. So how is this different from a public class?"

Reference question: What is the exact meaning of export keyword in Angular 2\TypeScript?

Example:

export class Product {
constructor(
public id?: number,
public name?: string,
public category?: string,
public description?: string,
public price?: number) { }
}

Upvotes: 2

Views: 2748

Answers (1)

Venkey
Venkey

Reputation: 371

History

ES6/ES2015 (ECMA Script) introduced module system natively to the language. Before ES6, JavaScript applications used libraries like requirejs for a module system implementation.

modules

Classes, functions, constants etc. can be exported from a module, and imported into other modules. Something that isn't exported is internal to the module.

Even though TypeScript has a similar concept before ES 2015, the language adapts ES6 module system to maintain consistency and standards. Read more here. https://www.typescriptlang.org/docs/handbook/modules.html

without modules, yesteryear applications used "script" elements carefully ordered, so that something is declared first, only then used in next few files. Also something declared in a previous script file, is not overridden with a new variable.

Classes

On the other hand, a class is an object-oriented programming concept that encapsulates state (fields) and behavior (functions). Access modifiers control how fields are available on an instance of a class. Public (default), private (internal to the class) and protected (accessible within the class and the derived classes). More here, https://www.typescriptlang.org/docs/handbook/classes.html#classes

Summary

In a nutshell, import classes from a module, create an instance, use public methods and properties. We can also import functions, constants, enums etc. They may be part of a module. May not provide encapsulation and abstraction like classes.

Upvotes: 2

Related Questions