#28 What is this? | JavaScript

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens my name is let's continue with the series on javascript and in this video we'll talk about this keyword i know that was pun intended the thing is this is actually a very simple concept but then it takes some time for your brain to you know get used to it so we'll divide this into two parts so we have two videos actually the first video we'll talk about what is this keyword and the second video we'll talk about why it is called this keyword okay then it will make much more sense so if you remember the last example where we have created this object so if you can see we have this object which is laptop and then this laptop has some properties right now this property i mean we have let's say cpu ram and brand and then we created a method as well so what is a method imagine an object having a property but it adds a behavior and we can add the behavior with the above function and that behavior for an object is called a method okay this is a method here and then we are trying to call that method right so this is the simple thing we have done and just to see what happens when you when you run this code let's try so beyond this code and you can see we got hello world so i don't want to print hello world here what i want to do is i want to print the values of the object right so the value we have here is let's say i'm the cpu ram and brand i want to print those things and let's say not even with a function called greet or method called greet i will say uh get config so let's say if i have this method which is get config and every time i call get config should print the configuration so of course the method name here should also be getconfig not great now what do you think will it work let's try so what i will do now is i will just remove this part from here and let me replace this with the help of the variables of course we want to print the variables right oh the properties so let me print cpu and let's see what happens if i try to print a variable cpu and let's see what happens and we got an error it says the cpu is not defined i know that's weird right cpu is defined there it's here but it is still not for able to find it okay maybe there's a problem with the variable outside the functions or method right let's create a variable inside it so i will say let storage that's a variable i want to create and let's say i will say this is hundred uh and then here let me print storage let's see if we can actually use a variable inside a function i don't know if we have done that before but let's try this time so let's run this code and you can see we got 100 so that means there is no issue of using a variable or printing a variable inside a function it is able to fetch this variable which is storage but not cpu you know why first of all cpu is not a simple variable it's a property of an object right so if you want to access that here we need to do something else we have to let's again let's type cpu but then if you really understand this cpu is not a part of the function here right it's a part of the object it's an object property in that case you have to use the object name so i will say hey this cpu belongs to an object and the object name is laptop that's simple so the object name which is a laptop dot cpu let's see if that works and that works you can see that every time you want to use this property you have to use a laptop name okay this makes sense right but the problem is see you can have multiple objects right let's say this can be laptop one right and then you can have laptop two then you can have laptop three there are different ways of getting object this is one one way of creating an object you can create an object with the help of a class where you don't actually define a object like this you don't say laptop one and then the properties then laptop two then the properties of course right laptop will have the same properties every laptop will have a cpu they will have a ram it will have some brand and you will have for some function yeah the value will change i mean they might have a different cpu they might have different uh ram they might have different brand but the point here is when you try to use an object to call a property uh using the laptop or the object name is not something you should go for what is preferred is instead of using the object name we can set this now what is this so this means the object so whatever the current object you are referring to you might have multiple objects you might have laptop one laptop two in fact let me just try that so i will select a one and then here i will let me just say laptop one and it should work i guess oh there's an error uh the other is because of this laptop here then it should be laptop one as well and let me run this code and you can see we got i9 what if i have one more let me just create it on top so that it will not be in our focus and let me just name this as laptop 2 and here also i will say laptop 2. but if i go down and if i say this cpu i want it from laptop 1 and you can see we are talking about the actual object we are using the name of the object and it should work there should not be an issue but there is one more thing what if you say this is i7 okay and here you are coming back and say this is laptop two so basically you wanted to refer to the same object but since you have a choice you went for another name which is laptop 2 and let's see what happens and you can see we got i7 we don't want that right we want this cpu to be the value coming from the same object so it's always better instead of using the object name use this keyword so this represents the current object okay what is current object so if you are in this method who is calling this method the laptop one is calling this method so based on which method or which object is calling that method it will refer this to that particular object what i'm saying is if you call this with laptop 2 now this will refer to laptop 2. let's try and you can see we got i7 it is happening because of this method here so of course when you call laptop2.getconfig you will get this method here uh so basically always use this keyword so what is this keyword this represents the current object so if you want to access any of the property of an object it's always better to use this keyword but why exactly this keyword why not something else that we'll see in the next video so i hope you are enjoying this let me know in the comment section and do subscribe for the videos [Music] bye [Music]
Info
Channel: Telusko
Views: 12,224
Rating: 4.9326315 out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: kE93zNJBCBw
Channel Id: undefined
Length: 6min 28sec (388 seconds)
Published: Tue Jun 22 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.