Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4913

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T04:23:08+00:00 2024-11-27T04:23:08+00:00

CLASSES | TS

  • 61k

TypeScript Classes

TypeScript adds types and visibility modifiers to JavaScript classes.

Members: Types

The members of a class (properties & methods) are typed using type annotations, similar to variables.

class Person {   name: string; }  const person = new Person(); person.name = "Jane"; 
Enter fullscreen mode Exit fullscreen mode

Members: Visibility

Class members also be given special modifiers which affect visibility.

There are three main visibility modifiers in TypeScript.

  • public – (default) allows access to the class member from anywhere
  • private – only allows access to the class member from within the class
  • protected – allows access to the class member from itself and any classes that inherit it, which is covered in the inheritance section below
class Person {     private name: string;      public constructor(name: string) {         this.name = name;     }     getName(): string {         return this.name;     } }  const person = new Person('Jane'); console.log(person.getName()); // console.log(person.name);  person.name isn't accessible from outside the class since it's private  
Enter fullscreen mode Exit fullscreen mode

Parameter Properties

TypeScript provides a convenient way to define class members in the constructor, by adding a visibility modifiers to the parameter.

class Person {     public constructor(private name: string) {         this.name = name;     }     getName(): string {         return this.name;     } }  const person = new Person('Birusha'); console.log(person.getName()); // console.log(person.name); Property 'name' is private and only accessible within class 'Person'  
Enter fullscreen mode Exit fullscreen mode

Readonly

Similar to arrays, the readonly keyword can prevent class members from being changed.

class Person {     readonly name: string;     public constructor(name: string) {         this.name = name;     }     getName(): string {         return this.name;     } }  const person = new Person('John'); console.log(person.getName()); // person.name = 'Jill'; // Cannot assign to 'name' because it is a read-only property.  
Enter fullscreen mode Exit fullscreen mode

Inheritance: Implements

interface Shape {     getArea: () => number; }  class Rectangle implements Shape {     public constructor (         private readonly width: number,         private readonly height: number     ) {}      getArea(): number {         return this.width * this.height;     } }   const r = new Rectangle(4, 3); console.log(r.getArea());  
Enter fullscreen mode Exit fullscreen mode

Inheritance: Extends

interface Shape {     getArea: () => number; }   class Rectangle implements Shape {     public constructor(         private readonly width: number,         private readonly height: number     ) {}      getArea(): number {         return this.width * this.height;     } }   class Square extends Rectangle {     public constructor(width: number){         super(width, width);     }      // getArea function is inherited }  const s = new Square(40); console.log(s.getArea());  
Enter fullscreen mode Exit fullscreen mode

Override

When a class extends another class, it can replace the members of the parent class with the same name.

Newer versions of TypeScript allow explicitly marking this with the override keyword.

interface Shape {     getArea: () => number; }   class Rectangle implements Shape {     public constructor(         public readonly width: number,         private readonly heigth: number     ) {}      getArea(): number {         return this.width * this.heigth;     }      public toString(): string {         return `Rectangle[width=${this.width}, height=${this.heigth}]`;     } }   class Square extends Rectangle {     public constructor(width: number) {         super(width, width);     }      // get area function inherited here      public override toString(): string {         return `Square[width=${this.width}]`;     } }   const rectangle = new Rectangle(20, 4); const square = new Square(30); console.log(rectangle.toString()); console.log(square.toString());  
Enter fullscreen mode Exit fullscreen mode

Abstract Classes

Classes can be written in a way that allows them to be used as a base class for other classes without having to implement all the members. This is done by using the abstract keyword. Members that are left unimplemented also use the abstract keyword.

abstract class Polygon {     public abstract getArea(): number;      public toString(): string {         return `Polygon[area=${this.getArea()}]`;     } }   class Rectangle extends Polygon {     public constructor (         private readonly width: number,         private readonly heigth: number     )      {         super();     }      public getArea(): number {         return this.width * this.heigth;     } }  const r = new Rectangle(43, 34); console.log(r.getArea());  
Enter fullscreen mode Exit fullscreen mode

beginnersprogrammingtypescriptwebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 2k
  • Popular
  • Answers
  • Author

    ES6 - A beginners guide - Template Literals

    • 0 Answers
  • Author

    Understanding Higher Order Functions in JavaScript.

    • 0 Answers
  • Author

    Build a custom video chat app with Daily and Vue.js

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.