FEWV minutes of TypeScript Classes
» Classes in TypeScript.
-
TypeScript, a superset of JavaScript, introduces the concept of classes to enable developers to write more structured and maintainable code.
-
Classes provide a blueprint for creating objects with predefined properties and methods. In this blog post, we will explore how to define properties, methods, and constructors in TypeScript classes, along with code snippets to illustrate the concepts.
» Properties
-
Properties are the characteristics or data associated with an object.
-
Class properties are variables defined within a class.
-
Here’s an example of a TypeScript class with properties:
class Person {
// Properties
name: string;
age: number;
}
- In the example above, the Person class has two properties: name and age. The name property has a type of string, while age property has a type of Number.
» Constructor
-
A constructor is a special method that is called when an object is instantiated from a class. It is used to initialize the properties of the class and perform any necessary setup.
-
Let’s modify our Person class to include a constructor:
class Person {
// Properties
name: string;
age: number;
// Constructor
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
}
-
The constructor here accepts a name and age parameters that have a type of string and number respectively and assigns it to the name property.
-
Within the constructor, we assign the parameter values to the corresponding properties using the this keyword.
» Class Methods
-
Methods are functions associated with an object, allowing it to perform actions or calculations. In TypeScript classes, methods are defined within the class body and can access properties and other methods of the class.
-
Let’s add some methods to the Person class:
class Person {
// Properties
name: string;
age: number;
// Constructor
constructor(name: string, age: number) {
this.name = name;
this.age = age;
}
// Class Methods
greet() {
console.log(`Hello, ${this.name} your age is ${this.age}.`)
}
}
- In the updated Person class, we added one method: greet(). The greet() method simply logs a message to the console, indicating the name and age of the Person.
» Using the Class
- Now let us use this Person class. To use the class we will have to declare a variable.
const person = new Person("John", 25);
-
Here, we created an instance of the Person class, passing the name “John” and age 25 to the constructor.
-
Calling the Class Method:
person.greet(); // Output: Hello, John your age is 25.
- Here, We then call the greet method on the person instance, which logs a greeting message.
» Conclusion
-
In this blog post, we delved into the world of classes in TypeScript and learned how to define properties, methods, and constructors.
-
Properties store the data associated with an object, while methods allow objects to perform actions. The constructor is responsible for initializing the object’s properties. By leveraging classes, TypeScript empowers developers to write structured and reusable code.
-
Remember, this is just the tip of the iceberg when it comes to working with classes in TypeScript. There are advanced concepts like inheritance, abstract classes, and interfaces that can further enhance your coding experience. So keep exploring and refining your skills to become a TypeScript pro!
I hope this cleared - Classes, it’s properties, Methods and Constructor in TypeScript. Thank You and Happy coding!😊