0% found this document useful (0 votes)
33 views

TypeScript Class 1

TypeScript classes provide templates for creating models, entities, and blueprints. A class defines properties and methods to encapsulate data and functionality. Classes can be declared or expressed. Class members include properties, methods, and a constructor. Functions and variables cannot be directly defined in a class. Static and non-static members differ in how they are accessed and allocated in memory. Access modifiers like public, private, and protected control visibility and accessibility of class members.

Uploaded by

Dhiraj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

TypeScript Class 1

TypeScript classes provide templates for creating models, entities, and blueprints. A class defines properties and methods to encapsulate data and functionality. Classes can be declared or expressed. Class members include properties, methods, and a constructor. Functions and variables cannot be directly defined in a class. Static and non-static members differ in how they are accessed and allocated in memory. Access modifiers like public, private, and protected control visibility and accessibility of class members.

Uploaded by

Dhiraj
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

TypeScript Class

In computer programming class is a Program Template.


Program Template provides pre-defined functionalities, which you customize
according to your requirements and implement.
Class can work as a
Model
Entity
Blue Print
Class is defined as a Model when it is mapped to Data.
Class is defined as Entity when it is mapped to business requirements.
Class is defined as Blue Print when it is used as factory. [set of functions and
values]
Class can be defined in 2 ways
Class Declaration
Class Expression
Syntax:
Demo.ts
//Module
// class declaration
export class Employee
{
// class members
}
// class expression
var Product = class {
// class members
}
Class Members
A class comprises of
Properties
Methods
Constructor
Accessor
Can we define a function in class? No
Can we define a variable in class? No
FAQ:
Why a function is not allowed in class? What is difference between function and
method?
Function is immutable, it can�t change its structure dynamically.
Class is a template, which can�t allow any immutable components.
Class can contain only mutable components.
Why a variable is not allowed in class? What is difference between variable and
property?
Variable is immutable
Property is mutable.

Static and Non-Static Members


Static refers to continuous memory.
Non-static (dynamic) refers to discreet memory.
Static members are accessible by using class name. [inside or out side the class]
Non-static members are accessible inside the class by using �this� and outside the
class by using
�instance of class� [object].
Static member is defined by using �static� keyword

Ex:
class Demo
{
static s=0;
n=0;
constructor(){
Demo.s = Demo.s + 1;
this.n = this.n + 1;
}
Print(){
console.log(`s=${Demo.s} n=${this.n}`);
}
}
let obj1 = new Demo;
obj1.Print();
let obj2 = new Demo;
obj2.Print();
let obj3 = new Demo;
obj3.Print();

Access Modifiers
TypeScript class members can be defined by using 3 access modifiers
public
private
protected
Public member is accessible from any location.
Private member is accessible only within the class.
Protected member is accessible within the class.
Protected member is accessible only in derived class by using derived class object.

�public� is the default access modifier for members in class.


Syntax:
class Product
{
public Name:string;
private Price:number;
protected Stock:boolean;

Print(){
console.log(`Name=${this.Name}\nPrice=${this.Price}\nStock=${this.Stock}`);
}
}
class Derived extends Product
{
Print(){
let obj = new Derived();
obj.Name;
obj.Stock;
}
}

Properties, Methods, Accessors and Constructor

You might also like