Class

  • Classes are object blueprints

  • Each instance of a class has its own unique properties

  • One class => One / Many objects

Example: Basic Class

Classes are defined with the class keyword, and instantiated with the new keyword

class Person {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    introduce(){
        return "Hello, I'm " + this.name;
    }
}

var p = new Person("Hemant");
console.log(p.introduce());

Inheritance

  • Classes can inherit properties from other classes

  • Child classes inherit all method and properties

Public / Private Modifiers

  • Class properties are public by default

  • Public properties can be accessed through object[propName]

  • Private properties cannot be accessed externally

Accessors (Getters/Setters)

  • Let you define special functions for accessing or setting a variable

  • Looks like a normal public variable from outside the class

Note: setters/getters are supposed to be used as property not as function

Static Properties

Static properties belong to the class itself, and not an instance

Last updated

Was this helpful?