#27 What are Methods in JavaScript?

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] welcome back aliens smartman720 and let's continue with the series on javascript in this video let's see how do you add a function to an object see we have talked about objects we have talked about functions and then we know the basics of it right see every object will have two things we have talked only about one thing there uh so object will have the data an object will also have the behavior see as an object right as a human or the alien we are the object right so we have two things we know something and we do something we know something because of the data which you which is stored here the property values and the names of your friends the favorite movie the concepts which you learn in javascript everything is stored here and the way you talk the way you walk the way you dance is all your behavior right and the way you behave is sometimes depend upon the way the things you know right so these two things are related to each other what you know and what you do so what you know is with the help of variables and what you do is with the help of functions so an object will also have the function right but then we don't call them as a function we call them as a method it's ultimately a function but technically we call it as a method okay so how do we do that so first of all let me remove the code and in this code basically we will be creating an object which we have done earlier the same object or let's go for different object this time let's go for laptop yes we have worked with laptop but it was a part of the alien object right but this time let's create a laptop object outside and the way you do is very simple you say let laptop and then equal to you you want to get an object here right so we'll use uh cut the bracket open close there are different ways of getting object we have seen one this is the second way uh so what we'll do now is we got this object right which is laptop uh let laptop and here let me create some properties okay what are the properties we need so let's go with cpu so the value for cpu let's say is i9 and then we'll also mention the ram so in this case let's say ram is 16 gb and then what is you need maybe you or we can also go for the brand name we'll say hp so basically we got an object right and i want to add a function here now as i mentioned before we will call it as a method but ultimately that's a function right now how do we do it now first of all even the method will be our property right so let's mention the property name here so maybe i want to have a function which will print hello world as of now let's go with that so i want to get a function inside this laptop which will print hello world so the way you can do that is by saying get value or maybe we can also say greet just to keep it simple and this greet will have a function right so we have to give a color not equal to we have to give a colon the same thing we have done for the function expression right we have to use a variable name and then we give equal to then we mentioned the function right here also we have to mention the property then we'll say function and then we can perform the option here what i want to do here is i want to say return or maybe i can just print hello world okay so we got a function here right and then we have some issue here okay and the issue is we forgot to put a comma so as i mentioned before you know comma is very important here so we can also put a comma at the end which will not give you an error but we if we add a new next property it will get resolved automatically right okay anyway so once you've got this how do we execute this it's very simple the way you do it for the function we just have to say greet and we have to give the round bracket that's it but what do you think will it work let's try so i'll go back here and let me execute this i will say node space app.js so when you center you can see we got an error now why there's an error here it's because we are trying to call this grid function and it's not defined because it is inside this laptop object right so basically this is a method here not a simple function it's a method which belongs to an object so that object is a laptop here so we have to say laptop.greet okay let me just run this code once again and let's see what happens and you can see we got hello world so basically a function or a method belongs to the object here which is laptop and if you want to call it you have to use the object name now of course with this function you can pass some parameters you can return some values and we'll see that later but as of now we are trying to create a function or a method inside the object and you can get multiple functions here so you can say greet you can say get me the values or you can say calculate the speed of a machine or do the benchmark everything just add multiple functions and one thing to remember they are called methods but yes even if you call function no one will kill you for that so methods are function the same thing so yeah that's it from this video i hope you enjoyed let me in the comment section and do subscribe for other videos bye
Info
Channel: Telusko
Views: 82,239
Rating: undefined out of 5
Keywords: telusko, navin, reddy, tutorial, java, python, blockchain, django
Id: 4r72b1ScPLY
Channel Id: undefined
Length: 4min 40sec (280 seconds)
Published: Tue Jun 15 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.