understanding the class instance method in javascript for data structures and algorithms video #66

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello guys welcome back so in the previous lecture we have seen how we can create a class how we can create an instance of a class okay so in this lecture let's quickly see some instance methods so first of all what are the instance methods okay so you guys might have used it already you guys have used it multiple times but you are simply not aware of it okay so you guys have used arrays you guys have used objects in arrays you have used push pop everything right so let me show you as an a small example of it okay what are instance methods then we will see an example in our class which we have built in the previous lecture I think it's Zoom down right so let's say let data equals to empty array okay uh fine so if I use data dot push so where where do you think this push is coming from here in the data I have just defined an array right I have just defined an array I have never wrote any push method any pop method then how I got this push method that means this is an instance of array right so that array might have all the push pop like reduce sort everything related to the array right all those methods so this particular variable this particular thing is just an instance which is inher erting or we can say accessing the methods which we have defined not we uh like which are already defined in the array prototype right in the array blueprint from there only we are accessing these methods okay so let's say I want to add three so what will happen now in data it will get pushed that means that push method is uh adding this three in the instance which we have created from the AR a blueprint okay so this is an instance which we have created remember from the previous lecture I think here I've shown you like how we create the arrays okay so what happens is we Define a blueprint uh a method here in the blueprint let's say um let's create a function or in object we don't need any function name let's create get full name okay so what it will return let's return something called hi your name is and let's add these two this do full name right okay so it's simple as that now I have added a method in the blueprint right I have not added directly in this one or this one or this one okay and this is how the prototypal in inheritance works in the JavaScript okay so I have added a method in the blueprint now whichever instance I'm creating here okay they are all able to access this full name and this full name will act as it is independent method or it is that particular in instance individual method right so let me show you for P1 this thing will return return me ronas for P2 it will return me Raj shasana for P3 it will return return me joy okay so it will act as it is their own method right so let's say person right P1 dot so now if you see we have person which have uh what's say Ron Shasta and H okay now if I access dot get full name I am able to get this get full name method since I have defined it in the parent class or we can say the blueprint okay I am able to access it because of something called inheritance this instances are simply simply accessing the methods properties everything from their parent take it this way your parent have purchased a car okay the car end of the day the car came into your house only right so you will go outside you will Flex like uh we got a car I have a car okay you will not say like my parent have purchased this car this is not my car since you have access to that car you will tell everyone like we have a car okay my house got a car right so it's the same thing any function which we declare in the uh what to say in the parent or blueprint okay parent class or blueprint we can simply access in all the instances which we create from that particular class okay and that is what we call instance methods okay so as you can see after using the get full name I'm getting high your full name is ronak shasa if I use P2 okay I'm getting hi your full name is Raj Shas okay for P3 quickly your full name is Joy okay so it is acting like it is its own method right but but behind the scenes this method is defined in the parent of these particular variables these instances we can say okay so these instances are simply accessing those methods so that they can return their respective value that's it it's nothing more than that right so uh this is what we call instance methods okay uh now if I if I show you in terms of like buil-in data structures so I have I think created something here so let's create one more let data equals to empty array okay data dot push now I think you guys will be clear like where this push is coming from right so if I push anything in this 23 so it will not add in the function Constructor it will not add in the Prototype it will add in the instance only right that is how we get data 23 inserted in that data right now if you expand this okay you can see we don't have anything we just have the values in it okay but we have something called prototype here the Prototype that means the parent of this instance is array okay and if you expand it you will see all the methods are coming from its parent only okay since in JavaScript it is a well-known thing like array is also an object okay so array also have a parent if you go down and if you see here let me quickly expand it okay so array has a prototype that is object that means end of the day array is also an object right so here if you expand it you can see all the methods and properties which you can use in an object is prototype of local string in Constructor assign create freeze entries entries has own group by these are all properties of objects right so these properties we can inherit we can access in the arrays and these properties these methods sorry these methods whichever we have implemented in the arrays we can access in the uh child of the array so end of the day we can access all the methods and properties which are available in the parent or we can say in the Prototype of the data of this particular instance okay so this is how like uh we create the instance message and this is what high level overview like an instance method is all about okay so yeah now let's create one more uh instense method quickly so let's say I want to pass um what to say full name age right so let's say uh what we can do here full name we are getting we are getting age okay so let's set the gender okay set gender and here we have to pass okay so I'm creating this so you guys will be clear like uh we can pass the parameters also in it just like console log array everything okay so it's nothing complicated you just have to create the methods in the blueprint and then you can simply access them in the instance methods just like it is their own methods it's simple as that it is nothing complicated people have made it complicated I don't know why right so here let's say I want to set the gender okay so for that what I have to do I simply have to write one more this dot gender equals to for time being let's set it to null okay so for time being I have set this gender to n for the person in the person I have set this gender tunel so if if I'm getting any gender here okay I'll simply this dot gender equals to gender okay so we can use this method to set a gender and then we can access this property directly to see what is a gender okay so let's see that quickly so let's say for P1 P1 do setgender as you can see I'm already getting this set gender so let's set it to male or it will in instincts I apologize for that okay so we have set the gender to male fine now we can access the gender property which we have created right so in person as you can see okay I can show you here itself directly as you can see uh we have Age full name gender as male so these properties we can access directly as well okay P1 dot gender if I do this I'm getting male let's see for P2 gender I'm getting null because I haven't set anything for the P2 gender right so uh this is how we can access the properties directly but the best practices are to access them through the methods only okay so then it will not make any sense for writing these methods right if you if you if you want to set it directly you can set it nothing is stopping you from doing that okay so let me show you so if you want to set P1 do gender directly okay you can set it Nothing is Stopping You but the best practices are uh to set it through the or like to restrict the user from accessing the methods and properties directly take them through the methods to set any properties okay so P1 dot gender if you see I'm getting female now right okay so what I was uh trying to tell you gender okay so here let's write a method get get gender okay so here what simply we have to do return this dot gender okay that's it you don't have to do anything I'm simply returning what it will do it will simply check the gender of that particular person and it will simply return it okay so let's see this one as well quickly so P1 dot get gender not this get get gender okay so if I'm not passing anything okay so it's not returning anything get gender here I'm returning get gender I think okay so this is because I haven't said anything that is why it is it is actually returning but it is returning null right so let's quickly set the gender first set gender to male now if I use get gender okay as you can see I'm getting mail now okay so we have to restrict the user from accessing the properties and methods directly what we have to do we simply have to create the functions or we can say instance methods or in short methods which will do the operation and which will return return the output or something and why I am showing you all these instance methods because we are going to use a lot of instance methods while lighting data structures while creating data structures that is why it is important to understand and how we can create instance methods how we can access them okay we can write complex logic in the instance method as well but I don't want to confuse you right now okay my goal is to make everything clear not to confuse you a little bit more right so this is how we can create instance methods and this is how we can simply uh use those instance methods right so I think let me just see if I can copy all these things or even if we don't even if we can't copy then that's not an issue at all so if you guys want you can take a screenshot of this okay and then you can take a screenshot of this one okay so you guys will get the idea right so p1. get gender p1. setgender p1. get gender again we are doing that for male and then I'm expanding it so if you want you guys can take an a screenshot of this and you are good to go you just have to write it you just have to practice it on your own once before we dive into the next lecture right so yeah this is it for today see you in the next one [Music]
Info
Channel: Coddict Buddies
Views: 43
Rating: undefined out of 5
Keywords: coddict buddies, JavaScript closures, JavaScript array methods, JavaScript design patterns, JavaScript arrow functions, JavaScript promises vs. callbacks, JavaScript functional programming, JavaScript responsive design, javascript class methods, javascript class example, class in javascript, constructor in javascript, javascript class properties, javascript class variables, difference between static method and instance method in javascript, javascript classes and objects
Id: GY7rgSl5_as
Channel Id: undefined
Length: 14min 20sec (860 seconds)
Published: Tue Apr 16 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.