Encapsulation Getters & Setters

Developed with
QA

Contents

keyboard_tab
Typescript
1
Inheritance
PREVIEW1m 24s
2
Access Modifiers
PREVIEW4m 17s
4
Abstracts
1m 52s

The course is part of this learning path

Typescript: Zero to Hero
course-steps
8
certification
2
play-arrow
Start course
Overview
DifficultyBeginner
Duration13m
Students66
Ratings
5/5
starstarstarstarstar

Description

This module introduces you to Classes and how to use them in TypeScript.

Learning Objectives

The objectives of this module are to provide you with and understanding of:

  • What classes are
  • How to use inheritance in TypeScript Classes
  • How to use Access Modifiers
  • How to use Abstracts
  • How to use Structural Typing

Intended Audience

This learning path is aimed at all who wish to learn how to use TypeScript

Prerequisites

It is essential you understand the face of contemporary web development to attend this course. You should have a good working knowledge of Javascript. Please note: Before attending this class delegates must have a Microsoft account (signing up one is free).


Feedback

We welcome all feedback and suggestions - please contact us at qa.elearningadmin@qa.com to let us know what you think.

Transcript

If we're implementing object-oriented principles using TypeScript, then we should really implement encapsulation, hiding the implementation of our class and applying logic to mutation. Indeed, we've already started this process by examining access modifiers. These are of little use unless we provide functions to access and mutate the private and protected properties, though. JavaScript has implemented get and set functions on class structures since ES5 in 2009. They should be leveraged to provide getters and setters in classes. Although there is the gotcha* that the get and set function names cannot be the same as the properties they are related to. A way around this is to use the private notation, formally an underscore, but now a hash on any private fields and make them all private by default. If we use this new syntax, we can dispense with the private keyword as shown on line two. The traditional way around it was to put an underscore at the start of the name to indicate a private class variable, as shown on line three. We have an instance of the class created on line 27. And we try and directly access the hash speed property on line 29. This is rejected as it's private property. On line 30, we access the speed property, effectively calling the get method. Notice no calling parentheses. We then set speed by setting the speed property on the car to 100. Again, we're not directly calling the function here. The output of the console log is 100. Calling the accelerate function on the car increase the value of hash speed, but the line-to-set speed to 175 isn't followed through as it doesn't meet the logical testing the speed setter, it's greater than 150. So hash speed will still return 200. Using get and set presents a compatibility issue that you need to be aware of. The TypeScript compiler targets TS3 by default, and as get and set are ES5, we need to target ES5 using the minus minus targets, which if we're using the compiler directly, or in the TS config file. In a further complication, the compiler only recognizes hash as private notation when targeting ES2015 or above. This is not recognized by VS Code, though, so beware. One way to ensure all of this works would be to add people to your web app config to do the relevant polyfills.

Lectures

About the Author
Students3156
Labs8
Courses29
Learning paths6

An outstanding trainer in software development with more than 15 years experience as a Corporate and Apprentice Trainer, ICT Teacher and Head of Department, with a passion for technology and its uses. Continuing to develop existing and new skills and courses, primarily in web design using PHP, JavaScript, HTML, CSS and SQL but also OOP (Java), programming foundations (Using Python), DevOps (Git, CI/CD, etc) and Agile/Scrum. Practically minded, a quick learner and a problem solver with an attention to detail to ensure high quality outcomes.

Covered Topics