TypeScript Constructors

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
all right so I've simplified the code from the last lecture I simply remove the method get distance because we're not going to use it later in this section so here on the top we define a point class and then below that we initialize a point object now this code is a little bit verbose because we have three lines to create a point object and put it in a valid State what if this point object had a few other properties that we had to initialize like this and maybe a few more here is there a cleaner way absolutely so let me delete this first all right in object-oriented programming languages we have this concept called constructor so every class can have a constructor which is basically a method that is called when we create an instance of that class so let me show you how it works in the class I'm gonna add a method the name of this method is constructor this is a reserved keyword in typescript now this method can have parameters so X which is a number and Y which is also a number and then here in this method we can initialize these fields so what should we write here this Delta X we set it to this X argument that we get here and similarly this that Y we set it to Y now look we got the compilation error here because when creating a new point object we need to supply these values look at the error supply parameters do not match any signature of call target so here we need to supply the values for X and y 1 and 2 and with this we can simplify this code and get rid of these two extra lines and here's the end result now what if somewhere else in our program we don't know the initial coordinate of a point in other words what if I want to create a point object without setting these values is that possible yes absolutely but it's a little bit different from how you have seen that in other languages like C sharp and Java in sharp we can have multiple constructors in typescript we can't so the solution for this is to make these parameters optional so here after X I add a question mark and that makes X optional and similarly Y should be optional as well because once you make it parameter optional all the other parameters on the right side of that parameter should also be optional this is a rule by typescript and a lot of other programming languages so now look we don't have a compilation error when creating a point object without initial values hi thank you for watching my angular tutorial if you enjoyed this video please like it and share it with others also you can subscribe to my channel for free new videos every week this video is part of my complete angular course with almost 30 hours of high quality content where you learn everything about angular from the basic to the advanced topics all in one course so you don't have to jump from one tutorial to another in case you're interested we can get this course with a big discount using the link in the video description and if not that's perfectly fine continue watching as the next section is coming up
Info
Channel: Programming with Mosh
Views: 23,237
Rating: undefined out of 5
Keywords: Angular, angular 2, angular 4, angularjs, angular.js, typescript, react, object-oriented programming, oop, programming with mosh, code with mosh, typescript tutorial, learn typescript, typescript tutorial for beginners, typescript class
Id: tiWqCBXnWj0
Channel Id: undefined
Length: 3min 32sec (212 seconds)
Published: Mon Sep 04 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.