JavaScript Object Oriented Programming Tutorial Beginners - OOP in JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] the video and today i'm going to be making a tutorial basically going over um object-oriented programming using javascript so i'll go over the basics of classes and objects and all that kind of stuff and then i'll transition into talking about the four pillars of object-oriented programming and how they are implemented and how they are seen in javascript code and then at the end i'll also go over some github repositories which implement object-oriented programming in javascript and basically just to show some use cases that you might like some some examples of why you might want to use object-oriented programming in your javascript code so let's actually get started into the tutorial and we'll go through the basics until the end so object-oriented programming is actually a programming paradigm which tells you that you should divide your code into different objects and those objects contain fields and methods and when i say fields i basically mean variables and when i say methods i basically mean functions so what happens is those objects are connected that can be connected to each other and you can change data that is inside of them also there are many things that you can do when you divide your project into objects because you can basically because you can basically protect data from some objects or even share them between different objects and if you are new to object-oriented programming you might think that you've never seen object-oriented programming code before however it is present in most of the modern languages for example whenever you're coding into like browser javascript you're you're accessing a lot of objects and one of the main objects that you usually like one of the most used object that you access all the time is the dom the dom is an object containing everything in your page and you can see that there's different methods inside of it there's different fields there's different elements inside of it and that's basically the idea so also every single not like data structure every single data structure that exists in javascript or even the types in javascript most of them are defined as objects so for example if i were to come over here and say something like console.log and i said like type of um let's think of let's think of something i'm going to put an array here just an empty array if we were to console log this and by the way i'm using node.js which like it doesn't matter you can do this in normal javascript as well i'm just using node.js so i can just see the console over here and i don't need to go to the browser but if i were to console this over here you would see that the type of this array is actually an object which is really interesting right why would why would it be an object if i were to change this to a string like this if we were to console this this you can see it's a string but why is an array an object right actually a string is also an object it's a type which it's an object that is is filled and and created to be abstracted into this that we see right here however inside of it the underlining code between behind creating a string is actually a lot more complex that we might imagine and we have we have access to objects all the time also you probably already used something like this right if we were to create an object over here something like user and i set it equal to this this over here is an object you can put whatever you want over here you can put keys and and values right as you might know i can put here my name and it's equal to something like pedro right but you can also put functions inside of here you can put um get name and you can basically just put a function directly into this object right well this is literally what an object is in object-oriented programming it's basically a a parent which contains different fields so name is a field and methods so get name is a method so actually this isn't the way that we define objects if you're going to code in object oriented programming we actually do it create by creating classes and the reason for that is because it becomes a lot more um well structured you become it becomes a lot easier to read your code and also to manipulate it so that you can do whatever you want to do with it so what you can do is actually you can create different objects by using the class keyword the class keyword was all was actually only introduced in the ecmascript 2015 and before that um honestly i don't know how they wrote object like how they created stuff like this because i wasn't writing javascript in 2015 however nowadays what you can do is you can just define an object like this and an example that i will give to you guys is imagine we want to create a person right and what do i mean by a person we we want to create an entity or something that will be will have like common fields and common methods and we want to reuse that code many times so that's why we would create a person because people might be different but they will have some underlining similarities like each person might have a name each person might have an age so that's the kind of thing that we want to share right so if i create a class or an object called person we can just add those fields and methods that all like every single person has into this and it will become a lot more reusable so to create a class you can just say class and the name of the class so let's for now call this person because if i want to create a person like each person that i'm going to create it's going to be a person right so inside of here we can actually just add different properties like i can create a variable a field over here called name i can set it equal to i don't know pedro and i can also create a function here like get name and it's just a function which literally just returns the name like return name however there's one thing right also you need to put a like either variable or const but you can see that it's not working right and the reason for that is because we're actually not structuring our code correctly here when you create a class you need to make sure that you actually put a constructor and what do i mean by a constructor is literally when you run your class when you create your class your object when you instantiate your object the constructor will be the first thing that is that that runs and inside of it you can put different variables that are going to come when you instantiate a new person and basically set them so that they are accessible throughout your class and what do i mean by that well when i want to create a new person i want to pass for example a name and an age right i want to pass those two things so these are the only two things that are different from each person so if i were to create a person called pedro i would pass pedro and the age would be 19 right if i wanted to create another person like jack i would create jack and the age would be 26 right so how can we actually differ that the kind of person that we're creating well with the constructor you can just pass this two variables that we want inside of here so i can pass i can say that this each person will receive a name and an age however these two variables that we will receive over here they aren't accessible throughout your class they aren't like they can't be accessed and the way to make them access accessible is actually by using the this keyword and why do we need to use this well when you say this over here you can create like any variable you want inside of here and like i can create this dot h equals to 38 389 actually and like anywhere inside of your class you can just say this dot age and you can access or this variable right here because what you're saying is this over here is relating to the class and you're saying that this class has a variable called age and it's equal to 389. so we're not like anywhere inside of the class you say this dot h it knows automatically what you're talking about so this is how you actually define its variables um to make this variable right here that we get when we instantiate this class we can just say this dot name equal to name and this is something that i remember you that i used to get really confused in the beginning when i used to do object-oriented programming in java because like why would we write this.name equal name like why are we rewriting the variable the reason for that is exactly what i just mentioned this variables right here that we get when we instantiate the object it's not they're not accessible like anywhere else in the class so this is how we make them accessible so we need to do the same thing for the age so this dot h equals to h and now we have these two variables which allow us to create an actual person so in theory we already we're already done like we can just create we can actually use this object to create different people and to actually instantiate an object or in class you can just create a variable here so i'll create a person one for example and i'm going to set it equal to new person and you can see the the new variable right here is probably something um if you're new with object-oriented programming you haven't used this before basically when you want to create an instance of the class when you want to like create a new person you actually need to use the new um variable right here the new not variable the new keyword basically you're just saying that you want to instantiate a new version of the person class so inside of here we actually have to pass two variables and the two variables are the two variables that we passed in the constructor so i'll pass over here i want to create a person with the name of pedro and the age of 19. so now this is literally it we just created a person i can access the fields in this object right here so for example if i want to console.log my name i can say person 1 dot name and if i come here to my to my terminal and i just refresh this and i run the code again you can see that it actually correctly said that my name is pedro right so this is great um but actually this isn't something that you would like to do if you're writing if you're designing your project in an object oriented manner the reason for this is because you don't want to have full access to the fields directly from out the outside what you actually want to do is you actually want to create methods inside of your functions that basically just return those fields and i know this looks this sounds dumb but every cs student who had an object-oriented class before they know that their teacher definitely made them create this kind of methods and honestly um it's necessary to some extent so i'm going to teach you guys how to do that and what do i mean by that is for example if somewhere in my code i want to have access to the name of the person right then i literally just have to create a function called get name and it's literally just a function which its only purpose is to return the name so i can just say return.this.name and now instead of saying person1.name i can just say person1.getname and now i'm accessing a function inside of this class and if i refresh my page again you'll see that it correctly console log my name which is great so let's actually create one for age as well because you want to create it to for all the variables and fields that you want to have access to its values so let's say get age and this dot h so it should work with this as well we can also console.log um get age over here but now you might be wondering okay we just like this is too like too many lines of code for something that doesn't do much right um actually no because look at this first of all you can see that we're correctly console logging the age but most importantly now we create we not only created a person class but now we've created an object that can be reused to create many different people for example i can come over here and i can just copy this and i can just create a new person so i'll call this and let's just call them moyes keen a div completely different person this person should be like 23 and instead of console logging person one dot name actually i want to log both person one dot name and person2.getname so you guys can see that we're actually creating different people so let's actually save this and let's run our code again and you can see that we are correctly console.logging two different people because we are instantiated two different objects um that are completely different because we are passing different fields but this looks cool right but there's a lot more that we can do with object oriented programming and when i mean improve i mean let's make this a little bit more complex you can see we have a very simple class right there's only two methods or two functions and there's only two fields right however imagine that we want to create more stuff let's actually create a new object a new class that will take in the person class as one of its fields and you might be thinking okay that's weird but for example imagine we want to create a class called home right and this class contains a bunch of information about your house including all the people that lives inside of it so for example if i wanted to get a list of all the people that lives inside of my house i might want to get an array of person right forget what i mean this is the kind of stuff that you can do if you're coding in an object-oriented manner so how exactly do we do that well let's create a class right another class called um house i don't know if yeah yeah house let's call it house and as we did before we can create a constructor here and the constructor will take in some stuff one of the things that it will take in is probably um like i don't know address um i'll just make up the addresses when i create this but let's take an address then um let me think about this price i don't know i'm just i'm just inventing stuff on the spot but then most importantly we might want to have um residence right and residence is actually a list so this is the important thing right the resonance is a list of person so what we can do here is we just created our constructor so what we have to do is we need to do the same thing that we did over there we create our class fields so this dot address is equal to address so let me just come over here and say equal to address then this dot price is equal to price and this dot residence is equal to resonance okay so now we have our three fields so what exactly do we need now well we just created three fields and maybe we want to create a get for each of these fields so let's actually do that let's create our get address and i know this is annoying but it's just something that you have to do if you're going to code in object-oriented programming because it will make your code a lot more protected and also a lot more organized so let's just return this dot address then let's do the same for get price and then let's return this dot price and finally let's get residence right and it's going to basically just be a function like we did before and it's going to return this dot residence so now let's test this right let's just come over here and let's create a house so a house just a simple house and it's going to be equal to new house but inside of here we have to pass three pieces of information one of them is going to be the address i'm just going to put something like random this i know this isn't an address but i'm just going to put this then price let's say it's about 280 000 i don't know this hard reach people then residents so remember residence is supposed to be a list of people right a list of of the different people so let's put an array here however we currently don't have different people to put here so what we can do is let's create different two different people right two different persons right so let's create here um pedro it's equal to new person i'll just go a little bit down scroll down new person and the name is pedro and the age is 19 and then let's create my brother this is actually my brother his name is david and let's create a new person and his name is david he's 21 i think probably i don't remember 21. now we have two different people oh accidentally clicked on something yeah two different people that we want to make re them as residents of this house so what can we do well we can come over here and just pass pedro and david inside of our list and now we can just first of all console.log and if we want to console.log the resonance we can just say console.log house.getresidence let's see what this gives us right let's save this and let's run this code and as you can see similarly to what i mentioned before how objects work in javascript you can see that when we when we console log our list of residents we actually get different objects right so we get two person objects and each object contains different information for example it contains different functions it contains different fields but most importantly you can see clearly that we are actually um we actually have a list of two objects so this looks great right this is exactly what we wanted but let's do more stuff with this one of the things that you might want to do with um with the house is you actually want to add a resident imagine i get married right something that is not going to happen for a few years but imagine i get married then i want to add a resident so let's create a a method called add residence and it's going to be a function very simply over here and all we want to do is we just want to append a new resident to this list so how do we actually append a new resolution to this list first of all we want to take in a person as our argument to this function so let's take in resident as the argument and inside of here let's actually do this we have access to the residence array right the residence list so what we can do is we can just say this dot residence dot push because push is a is a function is a function to add new stuff to the array and we can just push the new resonance and maybe if we want at the end of this we can just return the the residence list but actually no i just want to show you guys um how exactly we can see if the resident has actually been pushed right so we just added this to our uh house object right so what we can do is we have your uh our beautiful house and we have only two people inside of it so let's come over here we can say get resident as you can see and it will obviously show as we saw before it will only show me and dave like david and i right because there's only two people inside of it but now let's say house dot add resident and let's actually add a new person so let's create a new person here um i'm gonna create paulo paolo is my university roommate so let me create new person and let's say paulo i think he's 19 as well so 19 and let's just put here we want to add paulo as a new resident after this is done we just want to return resident again right and i should actually call this residence instead of get resident because it's more than one like person right so i just want to come over here and i want to console.log the get residence and let's actually see if this worked right if we were able to add paulo as a resident right so let's check this let's run this code again and as you can see the first time it console logged at the top here it only had two people right this is the first array the first console log that we towed it to basically the console log only having pedro and david but then we added a resident and we have a new array over here which at the end contains paolo so this is great we were able to actually uh change the data mutate the data inside of our objects which is exactly what object oriented programming um tells you to do right um i feel like this would be a great example and if you're a new student you're you're learning object-oriented programming in college this is like probably what you're going to be doing a lot you have to kind of like make your mind think in an object-oriented programming manner because you don't see like people if you're coding in javascript you don't usually see people quoting uh like this right however i'm gonna show you guys at the end of this video various examples that are actually very fitting and that use this the same kind of design to make their programs either they're programming just a normal client-side javascript program or they're building an api with node.js and express there's many use cases of to using object-oriented programming and despite people not thinking of javascript as an object-oriented programming language you can definitely do a lot of stuff with it so now what i want to do is i just introduced you guys to some of the basics of object-oriented programming now i want to basically just go over the four pillars of object-oriented programming and demonstrate how they are applied in javascript so getting into the the four pillars of object oriented programming we want to talk about the first one so the first one that i actually want to introduce is abstraction and the reason why i want to introduce this first is because what we have done so far is basically what abstraction is so think about this abstraction in the simplest form is just um hiding implementation of like a complex piece of code so that like you don't have to rewrite the same piece of code or you don't even need to access that piece of code um if you just want to access like like the the code as a whole so i know that explanation doesn't seem like the best one however just think about this what we've done so far is we created two different classes right and imagine that i don't like i don't care about the person class i just care about the house right and inside of it i have the residence right so in theory when i say for example when i say when i just have the house and i say house one dot um add resident and i say something like new person and i add a person right here like pedro um and then like my age when it does something like this i'm basically abstracting the code for person because i don't know what's inside of person or at least i don't need to because i just know that i have to do this right here and this is all i need i'm just creating a new person and i'm adding it to the house so in theory a person who is coding like the house class or working with the house um object they don't need to see the code for the person because it doesn't matter for them or because it's abstracted it would be different if for example we had to for like for each resident we had to define all the different stuff that that we need each time that we actually add the new resonance so imagine that for example um a person had another field that isn't actually we don't actually need to get this from the constructor it's just something that we might want to have right so for example imagine we want to have um this dot um job right and it's a string it's a string just imagine like this right it's a string and if i want to add the job for the person i want to set the job for the person i can create a method over here called set job and just do it like this and now i can just um this dot job equals to whatever job i put inside of here right i'm going to put job over here and i'm just saying this the job equal to job so initially the person doesn't have a job because when you create a person you obviously don't have the argument in the constructor to add the job right so let's come over here and i'll just create this house like let house equal to new house i'll just add all the weird information that we have such as the address then what else um then let's add the price i'll just put whatever and then let's just add the list of residents which will start as empty so now let's add a resident to this house right we just add pedro and now what we can do is we can just say okay i want a console log the house dot residence right or not dot resonance let's actually just cancel out get resonance like this now let's take a look at this let's console log this and you can see that despite not defining job in the constructor we still have job as one of the stuff inside of here right now let's try something else let's like let's just come over here and add um let's actually oh yeah let's just come over here and actually create pedro so let page row like this equal to new person and let's just copy this stuff over here and paste it inside of here so now instead of adding new person i'll just add pedro like this and now over here i want to actually say well i want to get pedro and i want to set job equal to developer right now let's take a look at what happens when we console log the residence and as you can see perfectly the code for like we don't care about the code that sets the job we just care that like the person in our house has a job of a developer right so that's the cool stuff that's the actual cool stuff and let's test one more thing if i get pedro.setjob and i change its value after i actually added pedro to my house let's see what happens if i save this and i just node index.js you'll see that it works and why does it work you can see that we actually added on pedro to our house after like before we changed its job title so why is the changing the change in pedro affecting um the object in the house because that's the important stuff that that's the actual uh cool stuff that you can do with object-oriented programming you can you can mutate different objects so that you don't have to actually have direct access to the code for that class specifically to make a change that is completely um important in a different class so this is the abstraction you don't need to see the the code for person to be able to make a house so i hope this explanation was clear but if you have any doubts just leave a comment down below because i know how hard it is to grasp the concepts especially the four pillars of object-oriented programming so now let's go into the next one we're going to talk about is encapsulation and again similar to abstraction we've already seen some of encapsulation being like some of the print like the idea of the the principle behind encapsulation being used in the code that we've written so far so what exactly is encapsulation well encapsulation just means that you want to protect certain fields in a class or an object so that people outside or the people who access that object they don't access directly the field itself but methods that can change the field or just return its value so if you paid attention to what we've done so far you realize that um every time we create a get price or a get residence or even an ad resident like a get and set method which is what they usually call it they call they call this getters and setters which basically means we're if we want to receive the price if we want to see the value for price we don't actually access price itself we could do this in theory over here i can just say console log for example let me create a house i'll just say let house no actually i'll create a person let pedro equal to new person like this and i'll just pass pedro and like whatever age right i can in theory come over here and say console.log pedro dot age right i can do this but if we're truly following the the principles of object-oriented programming and you can see that this would work right this says my age but if we're truly following the principles of object-oriented programming then we wouldn't want to do this and the reason for that is because object-oriented programming prays over the fact that you need to be very secure with the code so i don't want to be able to directly change this however there isn't a very clear way of creating private variables in classes in javascript there is a way but it's not like it's not the best way of doing it so i'm gonna show you guys some alternatives the thing is if you want to make this age variable um something that is private um you can just not use the this class the reason why we can access this is because we're saying this dot name right if we didn't have this then we in theory wouldn't be able to access it whenever we instantiate a person class however we have to have this the the this keyword right because we want to access the name in other methods so in theory what we can do to make all of this private is just put all of this methods inside of the constructor and this is a hundred percent legal i can just put it over here and it would work however this isn't the like this isn't how like how this was meant to be so it wouldn't be considered um very like a organized code in my opinion so you could do it like this and then change all this variables to remove this keyword but i wouldn't recommend doing it like this i actually recommend that if you're working in a team which is using object-oriented programming in javascript you just use an underscore behind each variable that you actually want to make private so that will signal to the people like that are accessing your code that this shouldn't be a variable that that like they can access outside so you would just use this underscore over here and in theory you're not protecting against hackers i'm talking about protecting against people in your own team which can make mistakes and consequently can ruin your code right so when you put an underscore it's a way to signal them that you shouldn't be accessing this field individually and that's actually the best thing so that's why i mentioned in the beginning that for every field that you create or every useful field that you create you should have a get the like a get for the field and a set for the field so that's the important thing about encapsulation one of the most useful and definitely one of the most important pillars of object-oriented programming which is inheritance and you probably heard about inhabitants before it is a very common interview question for internships because it's something that we learn in college so it's something that is really important in the programming kind of environment because it allows you to do a lot of stuff and the main example that i can come up with over here on how inheritance work especially in javascript is let's think about this imagine that i want to create a new class or a new object called programmer right so a programmer is just a normal programmer we can put like the the their company the company they work for and maybe their salary or and also the language that they like the most right these are the things that we want let's create this constructor let's do the the thing as always just say this.company equals to company then i'm not going to create the getters insiders for for the programmer specifically but um you guys will see why we like just this is just for demonstration purposes just so you guys can understand inheritance but we're basically just creating a programmer and over here we might want to have like actually i'll just create something like a a method which is purpose is just to say hello i am a programmer so let's do this i'll just come over here and say um greet no say hi i'll create a method called say hi and it's literally just a simple method um which it literally just says um hello i am a programmer um i work for and let's put over here the company that you work for so to do that i'm actually going to use the backticks to you to implement javascript variables inside of it so i'll just say something like i work for um and i'll say this dot company right i'm basically just saying hello i am a programmer i work for this dot company and that's perfectly nice right this is great i i can say this if i came over here and i created a programmer so let programmer equal to um new programmer and i say that i work for i don't know twitch and my salary is a billion whatever and my language my favorite programming language is um let's say javascript right it's actually typescript but let's just leave it like this and i wanted to say something like programmer dot say hi so if i did this you'll see that it would work right if i just run this you'll see it says hello i'm a programmer i work for twitch so what happens here is that um we are we have direct access to the say hi method right but let's think about this a programmer is actually a person right i know that that sounds like dumb but yeah it's a person so maybe like a programmer like its purpose isn't just to say it's the company they work for its salary and their favorite programming language every programmer has much more about them than this for example i might want to have know their age or their name but how exactly do i know that do i just add those fields over here and suddenly our constructor takes in like a billion different arguments no what i can do is i can actually say that the class of programmer extends the person class and what this means is programmer is is a person so this is the best explanation i can come up with a programmer will have access to all of the person like the the person methods and client like methods and fields that a person has so what exactly we can do is we can just come over here and let's think about this a person takes in a name and an age right so we can come here to our programmer constructor and we can pass a name and an age but most importantly we're not going to say this dot name equals name or this dot age equals age because the code that like that that treats this the name of the age isn't really like we shouldn't write the methods inside of programmer they should exist inside of person so what we can do is we can actually just say super and the super keyword basically says okay our super class or our parent class which is person we want to pass the following like we want to call this constructor when instantiated and pass the following values so i want to pass name and age so i'm basically basically just creating a person whenever i create a programmer so super just creates the an instance of the parent class and i'm passing to its constructor the following values and this is great because now what we can do is we can come over here and say something like hello i am a programmer my name is and instead of just not having your name over here you can just say my name is this dot get name and you can clearly see that it's giving me some autocomplete which is weird right because there's no get name to this class to the programmer class however since we just did this since we said we said that it extends the person class now we have full access to whatever class exists to whatever methods and fields exist in our person class so this is great we can just do it like this and let's just run this however we need to make some changes here we need to pass the arguments for for the person right so let's pass my name is pedro for example um my age is 19 and all the information so these two arguments are the ones that are going to be passed in the super to go to the constructor of our person class and now let's just run this again and as you can see it says hello i am a programmer my name is pedro and i work for twitch so you can see it works perfectly we have full access to whatever classes and methods we we which whatever fields and methods we defined on our super class and this is actually the most useful thing i believe from the four um pillars of object oriented programming this is the one where you're probably going to be using the most because it just saves you a lot of time it just abstracts a lot of code you need you don't need to like if you want to reuse code you can just do it this way so definitely is something that i would recommend learning a lot about object-oriented programming and honestly um i left this one to the to the end because it is probably one of the most uh one of the things that you will you will need to learn the least in javascript in my opinion by the way it's not don't take like take this with a grain of salt but i think that it is the thing that you should learn the least in javascript because it is very like it's it's everywhere when you code in javascript literally i'll show you guys an example of it what i'm talking about is is polymorphism and i know that this word sounds really weird sounds really difficult but literally what it means is um changing the form of something yeah i i think that's the the main definition is like something can take many forms and i'll give you guys a very clear example right now um if you go to javascript for example and i'll just uh like kind of like comment this out a bit if i came here to my javascript code and i said console.log and i said something like um one equals to one so you can clearly see that this aren't like this two things aren't equal right one is a number and this string over here is like despite saying it's one it's it's a string right it's not a number so let's console.log this to see what happens as you can see it says it's true and the reason for this is because javascript on its underlining code it does a lot of guessing it does a lot of guessing and changing like forms of stuff just so that it knows what you mean by them right and that this is exactly what i mean by why i like to code in typescript you define everything you don't like javascript have control over you but in this case you have to for example an example of polymorphism is also like just defining variables right if i create a variable called um i don't know name and i set it equal to a string called pedro if this isn't uh like a a type language like javascript this is polymorphism because it's basically guessing what type you want it's it's changing this could be name could be a number name could be 300 and whatever you can see right here it could be a number but so it means that it takes many different forms it can be a boolean as well like i can say it's false right so this is the idea um obviously it shouldn't make a lot of sense right now but like why we need to learn this but for example if i came over here and i said that i want to add um 300 and 40 plus um 432 right if i console log this you'll see that this over here will equal 340 432 so it basically converted 340 into a string and it just appended like it just united both of them together which sounds weird right but this is the idea it's just converting it's changing the form of this number right here so that it satisfies whatever like code that they wrote to make this happen in javascript they made this purposely so they used polymorphism to make this work but most importantly what is an example of polymorphism when you're working with classes right so when i'm working with um like this example right here i have a person and i have a programmer well let's let's let's try to create a different class right imagine that we had a different class over here that actually this class over here is not a programmer it's actually just uh i don't know let me think about this uh a doctor right a doctor is a person as well so why can't we just create another class another object called doctor which also extends person so doctor would also take name and age as the first ones but it might take something else like um area like i don't know like seller i'll get salary i don't want to waste time with this but basically it can take different constructor arguments as you can see right here and similarly to programmer it can access all the classes from person so like a person can takes into ma like it can take many it can take many forms right it can be a programmer it can be a doctor so this is the basic idea and there's many different types of polymorphism and the reason why i don't want to go that much in depth on it is because i don't want to i want you guys to think that this is as important as the other pillars because it won't be like being completely honest when you're working with your code you won't be thinking about polymorphism because probably if you're going to do stuff like function overloading which by the way is just like creating two functions with the same name um you probably won't find that like you won't encounter that the case very often right so unless you're working directly with a like very very strictly type language or a language like java which object-oriented programmer programming is everything then i don't recommend focusing a lot of your time with polymorphism and if you have any questions about polymorphism if you're a a university student and you have doubts just leave a comment down below i'll answer everyone so yeah this is the basic idea of polymorphism so now i'll just go over like one or two github repositories that i find that are using object-oriented programming to write their javascript code okay guys so first of all shout out to this person over here i have no idea who they are however um i just got this random github repository online and i think i can go over the code because it's completely public right so basically this is an express api written completely following the object-oriented programming principles so if you've coded an express api before you know that normally people don't teach you how to do it by creating classes that kind of stuff however i'm going to be honest the current startup that i'm working in we have to use classes we have to follow object-oriented programming principles so it's just something that you might encounter right i can't show the code for my work because it's not a public repository it's not open source however this one is just a random project which i think will help you guys understand a bit so in the src folder for their project let's look at where their server starts and as you can see they actually have everything that you would normally have if you're working with a normal express api but also they create a class so they're working through with all the things that i mentioned before they have their methods they have their as you can see set routes they have their getters and setters and they just work through everything by creating objects and at the end they don't export um i don't know the the express variable they actually export the class itself so that if they want to access it wherever they want to it just works right so for example i'll come over here to routes um let's see what they do with routes so they they they put everything into the controllers i guess the controllers will also have like will also be an object-oriented programming design um apparently no i maybe i just got a unfinished um project i think so yeah this isn't finished i'll just find another one right now okay guys so actually i just found this medium article which i'm going to link in the description if you guys want to check it out um it talks a lot about everything that i just mentioned it talks about abstraction polymorphism everything related to implementing and creating an object-oriented programming javascript program so i'm going to link this in the description if you guys want to check it out definitely go go check it out it looks like a really nice article i read a bit of it but i didn't obviously read everything but it looks like it talks a lot about how to implement and it's talking about express servers so it basically will go over how to implement the object oriented programming design principles while creating an express server so this is the basic idea i really hope you guys enjoyed this video if you enjoyed it please leave a comment down below it took me a long time to make this video i've been recording for like hours now but yeah i really hope you guys enjoyed this video um join my discord because i answer a lot of questions there i i'm currently doing kind of like a a project i i'm going to record a video at the end of this month where i just basically review my subscribers projects so if you want to submit it the link is in the description it just goes to the project gallery channel and just submit your project there because i definitely want to see it so i would really appreciate if you guys did that and subscribe because i'm posting three times a week and i would really appreciate it so yeah i really hope you guys enjoyed it and i see you guys next time [Music]
Info
Channel: PedroTech
Views: 56,117
Rating: undefined out of 5
Keywords: computer science, javascript, nodejs, programming, typescript, node js, pedrotech, traversy media, traversymedia, clever programmer, tech with tim, freecodecamp, object oriented programming, object oriented programming javascript, oop tutorial for beginners, oop in javascript, javascript oop tutorial, javascript classes, javascript objects, inheritance, encapsulation, polymorphism, inheritance in javascript, abstraction, object oriented javascript, oop javascript, programming with mosh
Id: GEuS0tfLfEY
Channel Id: undefined
Length: 46min 35sec (2795 seconds)
Published: Wed Jan 20 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.