4

100 days of TypeScript (Day 2)

 3 years ago
source link: https://peteohanlon.wordpress.com/2021/10/27/100-days-of-typescript-day-2/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
100 days of TypeScript (Day 2) – Confessions of a coder

In 100 days of TypeScript (Day 1), I created a basic TypeScript “application” where two numbers could be added together. This was intended to introduce you to get the pre-requisites in place for writing TypeScript code, as well as acquaint you with using the type system. I promised, at the end of the post, that I would rewrite this application to use the TypeScript class system instead.

Before I start writing the code, it is worth looking at what classes are, both in a general sense and then in how they relate to TypeScript/JavaScript.

Simply put, a class is something that allows us to group data and behaviour together into an object; this sounds complicated, but is actually really simple. The idea about using a class is that we put everything that we need to do in one place. Imagine that I want to group together everything to do with playing a guitar, so I create a guitar class. This has certain attributes (data) that applies to it such as the number of strings, the type of guitar it is, the manufacturer, model; possibly it has pickups, and so on. These could all be grouped together into one class called Guitar. It also has things that we can do to it, so for an electric guitar, we can choose which pickups we are using, we can adjust the tone controls or change the way the guitar is tuned. These behaviours or actions can all be added to the Guitar class. In other words, everything we need is grouped into one object.

Note: In a future post, I’ll demonstrate how we would break that Guitar class down into smaller classes that are much more specialist.

When TypeScript was first created, classes were something that were being considered for JavaScript, but which hadn’t actually been officially released. TypeScript allowed us to write code that used classes, and outputted code that JavaScript would use without having the class keyword; when classes were formally added to JavaScript, our TypeScript code could be recompiled to use them natively.

So, what does a class look like? For out addition class, we can start out with an empty definition that looks like this.

class Addition { }

This isn’t very useful as it stands, so I am going to add something called fields to represent the two numbers I want to add together. A field is just the name we give to the thing that holds the data for our class. Adding our fields looks like this.

class Addition {
number1: number = 0;
number2: number = 0;
}

In the code here, I have added my two number fields and given them an initial value of 0. I had to give them an initial value because TypeScript complains there is no value if I don’t.

Before I get into the part where I create our add method, let’s take a look at how I put values into number1 and number2. In order to get to these two fields, I have to do something called instantiation. So, Addition is our class – to use it, we create an instance of the class (this is why it’s called instantiation). To create an instance of our class, we use a special keyword called new. It looks just like this.

const additionInstance = new Addition();

With this, I now have the ability to set the values of number1 and number2 directly.

additionInstance.number1 = 10;
additionInstance.number2 = 20;

Going back to the Addition class, I am now going to write the addition method (In object-orientation, you can think of a method as the name we give to a function).

public Add(): number {
return this.number1 + this.number2;
}

In my Add method, I get access to the number1 and number2 fields with the special this keyword. What this does is give methods in the class access to other parts of the current instance of the class.

Now that I have completed the Add method, I can now call it from my instantiated code. The instantiation code now looks like this.

const additionInstance = new Addition();
additionInstance.number1 = 10;
additionInstance.number2 = 20;
console.log(additionInstance.Add());

And that’s it. We have created our first TypeScript class here. In the next tutorial, I will demonstrate how we can hide fields so they cannot be accessed outside the instance of the class and introduce constructors that help us to instantiate our classes with a bit more flair.

The code for this post can be found here.

Related

100 Days of TypeScript (Day 1)October 25, 2021In "TypeScript"

100 days of TypeScript (Day 3)October 28, 2021In "CodeProject"

Please select your collectionMay 21, 2009In "Windows Presentation Foundation"


Recommend

  • 10
    • dev.to 4 years ago
    • Cache

    100 Days of Code ~ Day 3

    100 Days of Code ~ Day 3 Well, did take a day off for the holiday, but made sure I was right back at it today! Completed the 3rd and 4th activities from JavaScript30. Activity 3...

  • 9
    • dev.to 3 years ago
    • Cache

    100 days of code - Day 2

    100 days of code Today, we'll go through the Symfony Routing component, which allows to set up routing in PHP applications. We can add this component to the project with this command...

  • 3
    • dev.to 3 years ago
    • Cache

    Day 1: 100 days of code

    1. How to center an div element vertically or horizontally. There are many ways to center an div element, few of them are:- Method-1: Using margin:auto (For horizontal centering) In this method, if th...

  • 13
    • www.codeproject.com 3 years ago
    • Cache

    100 Days of TypeScript (Day 1)

    Introducing TypeScript It’s been a while since I’ve had the chance to write in this blog and I really wanted to come up with something different for me. If you’re a long time follower of mine, you probably know that I wrote a book o...

  • 5
    • peteohanlon.wordpress.com 3 years ago
    • Cache

    100 days of TypeScript (Day 3)

    Welcome back to a series about learning TypeScript from basics through to some pretty advanced stuff. In Day 2, we learned how we can create a simple cla...

  • 2
    • www.codeproject.com 3 years ago
    • Cache

    100 Days of TypeScript (Day 4)

    Wow. Day 4 and we have already covered a lot of material in our quest to go from zero to, well not something cliched, with TypeScript. In the last couple of posts, we delved into using classes in TypeScript so, in this post, I would like to t...

  • 8

    100 days of TypeScript (Day 4) – Confessions of a coderSkip to content Wow. Day 4 already and we have already covered a lot of material i...

  • 8

    On day 4 of our journey into TypeScript, we looked at how we could use interfaces to act as data types. As I am about to show you, that’s not all that in...

  • 4

    On day 5, we looked at how to implement interfaces to say what behaviour a class can have. I said, at the end, that we were moving into the territory of...

  • 6
    • peteohanlon.wordpress.com 3 years ago
    • Cache

    100 Days of TypeScript (Day 7)

    First of all, I’d like to apologise for the delay getting around to this post. There’s been a lot going on that’s got in the way but I haven’t forgotten about writing this series. In

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK