0% found this document useful (0 votes)
13 views5 pages

TS Classes

The document discusses the basic structure of classes in TypeScript and examples of using properties, constructors, access modifiers, and getter/setter methods with classes. It provides an example of a basic Customer class only using properties, a class using a constructor to set properties, and the differences between the two approaches. It also covers public, private and protected access modifiers and demonstrates how to use getter and setter methods to access private properties.

Uploaded by

surya s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views5 pages

TS Classes

The document discusses the basic structure of classes in TypeScript and examples of using properties, constructors, access modifiers, and getter/setter methods with classes. It provides an example of a basic Customer class only using properties, a class using a constructor to set properties, and the differences between the two approaches. It also covers public, private and protected access modifiers and demonstrates how to use getter and setter methods to access private properties.

Uploaded by

surya s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Basic Structure

class Customer {

// properties

// constructors

// getter / setter methods

==================================================================================

Example of class only using properties

class Customer {

// properties

firstName: string; # properties are public by default

lastName: string;

// now let's use it

let myCustomer = new Customer(); #construct an instance using “new” keyword

myCustomer.firstName = "Martin";

myCustomer.lastName = "Dixon";

console. log(myCustomer.firstName);
console. log (myCustomer.lastName);

Output
>node cluster.js

Martin

Dixon

Example of class using constructor

class Customer {

firstName: string;

lastName: string;

constructor(theFirst: string, theLast: string) {

this. firstName = theFirst;

this.lastName = thelast;

// now let's use it

let myCustomer = new Customer (“Martin", "Dixon");

console. log(myCustomer.firstName);

console. log(myCustomer.lastName);

Output
>node cluster.js

Martin

Dixon
Difference between class only using properties and class using
constructor
While passing value to the class

Class only using properties


let myCustomer = new Customer();

myCustomer.firstName = "Martin";

myCustomer.lastName = "Dixon";

Class using constructor


let myCustomer = new Customer (“Martin", "Dixon");

Access Modifiers

Public - Property is accessible to all classes (default modifier)

Protected - Property is only accessible in current class and subclasses

Private - Property is only accessible in current class

Example of code with private and without setting a getter and setter

This will not give an expected result

class Customer {

private _firstName: string;


private _lastName: string;

constructor(theFirst: string, theLast: string) {


this._firstName = theFirst;
this._lastName = theLast;
}
}

// let's create an instance


let myCustomer = new Customer("Martin", "Dixon");

myCustomer.firstName = "Susan";
myCustomer.lastName = "Public";

console.log(myCustomer.firstName);
console.log(myCustomer.lastName);

For setting the getter and setter

class Customer {

private _firstName: string;


private _lastName: string;

constructor(theFirst: string, theLast: string) {


this._firstName = theFirst;
this._lastName = theLast;
}

public get firstName(): string {


return this._firstName;
}

public set firstName(value: string) {


this._firstName = value;
}

public get lastName(): string {


return this._lastName;
}
public set lastName(value: string) {
this._lastName = value;
}

// let's create an instance


let myCustomer = new Customer("Martin", "Dixon");

console.log(myCustomer.firstName);
console.log(myCustomer.lastName);

You might also like