Lesson Overview

Hello there! Today, we're delving into TypeScript classes, focusing on attributes and methods. Attributes define properties, whereas methods define actions. Through the Dog class, you'll learn these concepts. By the end of the lesson, you should be capable of effectively using attributes and methods in TypeScript classes.

Understanding Attributes in TypeScript Classes

Attributes can be regarded as properties. For a Dog class, the attributes can include name: "Fido", breed: "Poodle", color: "White". These attributes depict a dog's characteristics, such as those of Fido.

Creating Attributes in a TypeScript Class

In the Dog class, we define attributes like name, breed, and color directly. These attributes represent a dog's characteristics and are set with default values:

TypeScript
class Dog {
    name: string = 'Fido';
    breed: string = 'Poodle';
    color: string = 'White';
}

To instantiate the class and access these attributes, you would do the following:

let fido = new Dog();

console.log(fido.name);  // Prints: Fido
console.log(fido.breed); // Prints: Poodle
console.log(fido.color); // Prints: White

Here, Fido is an object of the Dog class.

Understanding Methods in TypeScript Classes

Methods are actions that instances of a class can execute. For the Dog class, behaviors might include bark(), eat(), and sleep(). These methods are functions defined in the class.

Creating Methods in a TypeScript Class
Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal