2

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) { }
}
5
  • 1
    export so you can import in another class, public so that you can access the class from another class Commented Sep 29, 2019 at 23:07
  • 1
    Making something public does not automatically make it available to import. You need to export it to make it available for import in other files. This isn’t an angular thing, it’s a typescript/es6 thing. Commented Sep 29, 2019 at 23:08
  • hi @AlexanderStaroselsky ok, feel free to write as answer, and I can send points, thanks Commented Sep 29, 2019 at 23:09
  • hi @joyBlanks see comment above, thank you Commented Sep 29, 2019 at 23:11
  • 1
    export so you can import in another class, public so that you can access the class from another class. Even though you have to import it to access it. if it is made private even after importing you can't access it. read more about access modifiers Commented Sep 29, 2019 at 23:13

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.