What is a class and how do we define one?

At the core of object-oriented is the class. A class is a definition of what is available for an object once instantiated. A class holds variables and functions that were judged cohesive by the developer. A class can share information across all instances of the same class or have its own data, which is unique from the beginning of the life of the object until its death.

The creation of a class starts with the keyword class followed by the name of the class. It is similar to the creation of an interface:

export class MyClass {
}

A class can define variables and functions. Each of them is public by default, meaning that they are accessible from outside the class by the name of the instance:

export class Variables {
public a: number = 1;
private b: number = 2;
protected c: number = 3;
d: number = 4; // Also public
}

Once a class is defined, it can be instantiated. An instantiation means that a class becomes concrete and the life cycle for its content starts. To create an instance of a class, the new keyword must be used. After new comes the name of the class with parentheses:

const d = new Variables();
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.61.19