Class in term of OOPs is a blueprint for creating objects.
TypeScript supports object-oriented programming features like classes, Interfaces, Polymorphism, data-binding etc. Object means this a real world entity. JavaScript
ES5 or earlier didn’t support classes. Typescript inherits this feature from
ES6.
In class group of objects which have common properties. Class contains fields, methods, constructors, Blocks, Nested
class and interface.
Syntax to declare a class:
class class_Name{
field;
method;
}
Here, in place of class_Name, name will be there, and inside field and method or function.
If we compile this, It will generate JavaScript code.
// Generated by typescript 1.8.10
var Person = (function () {
function Person() {
}
return Person;
}());
We will see a simple example:
class Person {
perCode: number;
perName: string;
constructor(code: number, name: string) {
this.perName = name;
this.perCode = code;
}
getGrade() : string {
return "A+" ;
}
}
The example declare a Student class which has two fields that is
perCode and
perName and a constructor which is special type of function which is responsible for variable or object initialization. Here is parameterized constructor. And
this keyword which refers to the current instance of the class.
getGrade() is a simple function.
The TypeScript compiler will convert the above class in JavaScript code:
Objects
An object is an instance of class which contains set of key value pairs. It's value may be scalar values or functions or even array of other objects.
The syntax is given below ?
var object_name = {
// scalar value
key1: “value”,
// functions
key2: function() {
},
// collection
key3:[“content1”, “content2”]
};
An objects can contain scalar value, functions and structures like arrays and tuples.
We can represent object in literal notation:
var person = {
fName:"Marry",
lName:"Jerry"
};
// Access the object values
console.log(person.fName)
console.log(person.lName)
Marry
Jerry
Let's see with simple example:
var person = {
fName:"Vivek",
lName:"Hanks",
// Type template
Hello:function() { }
}
person.Hello = function() {
console.log("hello "+person.fName)
}
person.Hello()
Output:
hello Vivek
Objects as function parameters
var person = {
fname:"Vivek",
lname:"Ranjan"
};
var hello = function(obj: { fname:string, lname:string }) {
console.log("first name :"+obj.fname)
console.log("last name :"+obj.lname)
}
hello(person)
Output:
first name :Vivek
last name :Ranjan
For creating Instance Objects.
To create an instance of the class, use with the
new keyword followed by the class name. To allocates memory for objects with the help
new during runtime. Memory allocation for all objects in heap memory area. given below:
var object_name = new class_name([ arguments ])
see with simple example declaration:
// Creating an object or instance
let obj = new Student();
Accessing Attributes and Functions:
A class’s attributes and functions can be accessed by the object. With the help of ‘ . ’ dot notation we access the data members of a class.
// Accessing an attribute
obj.field_name
// Accessing a function
obj.function_name()
We will see with the example:
class Car {
// Field
engine:string;
// Constructor
constructor(engine:string) {
this.engine = engine
}
// Function
display():void {
console.log("Function displays Engine is :"
+ this.engine)
}
}
// Create an object
var o1 = new Car("geeks")
// Access the field
console.log("Reading attribute value Engine as: "
+ o1.engine)
// Access the function
o1.disp()
After compilation this will convert into JavaScript code look like:
//Generated by typescript 1.8.10
var Car = (function () {
// Here is constructor
function Car(engine) {
this.engine = engine;
}
// Function working
Car.prototype.display = function () {
console.log("Function displays Engine is: "
+ this.engine);
};
return Car;
}());
// Create an object
var o1 = new Car("geks");
// Access the field value
console.log("Attribute value Engine as: "
+ o1.engine);
// Access the display function
o1.display();
Output:
Attribute value Engine as : geeks
Function displays Engine is : geeks
Similar Reads
TypeScript class A TypeScript class is a blueprint for creating objects, encapsulating properties (data) and methods (behavior) to promote organization, reusability, and readability.Supports inheritance, allowing one class to extend another and reuse functionality.Provides access modifiers (public, private, protecte
4 min read
TypeScript Generic Classes Generics in TypeScript allow us to create reusable and type-safe components. Generic classes help in defining a blueprint that can work with different data types without sacrificing type safety. They enable better code reusability and flexibility by allowing us to define type parameters that will be
5 min read
Class Decorators in TypeScript Class decorators are special functions that can modify or enhance the behavior of a class. They act as wrappers around a class, allowing you to inject additional functionality without altering the original class code. This is particularly useful for adding common functionalities across multiple clas
3 min read
TypeScript Narrowing Assignments TypeScript narrowing assignments is a type of type inference mechanism that occurs when TypeScript examines the right side of an assignment and uses that information to narrow down the type of the variable or property on the left side of the assignment.Syntaxlet value: string | number = valueOftheVa
3 min read
Typescript Abstract Class TypeScript, a superset of JavaScript, introduces many features to improve code organization, readability, and maintainability. One of these features is abstract classes. This article will explain what abstract classes are, how they work, and how to use them effectively in your TypeScript projects.Th
3 min read
TypeScript Cheat Sheet TypeScript is a strongly typed, object-oriented, compiled programming language developed and maintained by Microsoft. It is a superset of JavaScript, adding static types and other powerful features to improve development efficiency and code quality. TypeScript is widely used in web development, espe
6 min read