This is the 7th day of my participation in the November Gwen Challenge. Check out the details: The last Gwen Challenge 2021

Examples of TypeScript

The constructor constructor, and the Setter and Getter.Copy the code
The constructor constructor

Usually when we define a class, we don’t know what the value of the property is. What if we want to assign the value at instance creation time? Class has a constructor() method, which is executed when an instance is created (the moment of a new operation).

/ / 1
class Person {
    public name: string;
    constructor(name: string) {
        this.name = name; }}const preson = new Person2('bear');
preson.name    // bear
Copy the code

The constructor() method receives this value, which the constructor receives and assigns to the name property. Example 1 above can be abbreviated to example 2 below.

// Example 2
class Person {
    constructor(public name: string){}}const preson = new Person2('bear');
preson.name    // bear
Copy the code

When both a parent class and a subclass have constructors, be sure to call the parent class’s constructor manually in the subclass via super().

/ / 3
class Person {
    constructor(public name: string){}}class Student extends Person {
    constructor(public id: number) {
        super('bear')}}const student = new Student(10);
student.name    // bear
student.id      / / 10
Copy the code

In example 3, the Student subclass inherits Person and must call the super() method with the arguments required by the superclass constructor.

Getter and Setter
/ / 4
class Person {
    constructor(private name: string) {}
    get getName() {
        return this.name; }}const person = new Person('bear');
person.name       Property 'name' is private and only accessible within class 'Person'
person.getName   // bear
Copy the code

In example 4, calling the class’s private property directly reported an error, but using getName would get the value normally. Note: Person.getName is not habitually written as person.getName().

You’re actually calling the Person class’s private properties indirectly from the outside. In a real project, you would normally process or encrypt the private properties and then expose them through the Getter.

/ / case 5
class Person {
    constructor(private _name: string) {}
    get name() {
        return this._name + ' hello'; }}const person = new Person('bear');
person.name    // bear
Copy the code

In general constructors we prefix private properties with an underscore (_) before the property name, such as _name in example 5, and getters we use the real property name. What looks like a direct call to the person.name property when called on the instance is actually the corresponding Getter for the call.

/ / case 6
class Person {
    constructor(private _name: string) {}
    get name() {
        return this._name + ' hello';
    }
    set name(name: string) {
        this._name = name  // It is usually assigned after processing, for example}}const person = new Person('bear');
person.name = 'panda';
Copy the code

Setters also protect the class’s private variables by processing the incoming values and assigning them to the corresponding properties.

Finish this! If this article helps you at all, please give it a thumbs up.