image
Encapsulation Getters & Setters
Start course
Difficulty
Beginner
Duration
13m
Students
644
Ratings
5/5
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
Students
22526
Labs
6
Courses
29
Learning Paths
16

Ed is an Outstanding Trainer in Software Development, with a passion for technology and its uses and holding more than 10 years’ experience.

Previous roles have included being a Delivery Manager, Trainer, ICT teacher, and Head of Department. Ed continues to develop existing and new courses, primarily in web design using: PHP, JavaScript, HTML, CSS, SQL, and OOP (Java), Programming Foundations (Python), and DevOps (Git, CI/CD, etc). Ed describes himself as practically minded, a quick learner, and a problem solver who pays great attention to detail. 

Ed’s specialist area is training in Emerging Technologies, within Web Development. Ed mainly delivers courses in JavaScript covering vanilla JS, ES2015+, TypeScript, Angular, and React (the latter is authored by Ed) and has delivered on behalf of Google for PWAs. Ed has also developed a new suite of PHP courses and has extensive experience with HTML/CSS and MySQL. 

Ed is responsible for delivering QA’s Programming Foundations course using the Eclipse IDE. His skillset extends into the DevOps sphere, where he is able to deliver courses based around Agile/Scrum practices, version control, and CI/CD.

Covered Topics