Learn JavaScript OBJECTS in 7 minutes! 🧍

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey what's going on everybody in today's video I'm going to discuss objects in JavaScript an object is a collection of related properties and or methods properties are things that an object has such as a first name or an age a method is a function that belongs to an object for example I have a person object our person can say hello and they can say by methods are just functions that belong to an object what can this object do objects can represent Real World objects such as people products or places in this example we'll be creating some people objects people from the show SpongeBob we'll start from scratch though we'll create a person object I'll use a constant although it's not necessary const person equals then add a set of curly braces we'll start with any properties you can add as many properties as you would like they're in key value pairs this person will have a first name key key colon then some value this person's first name will be SpongeBob separate each key value pair with a comma let's include a last name property last name colon space will be Square Pants add a comma when you're done with this key value pair We'll add an age property of 30 age has a different data type it's a number the first two in my example were both strings SpongeBob and Square Pants let's add a Boolean is employed colon space I will set that to be true he has a job at the K crusty crab okay that's good enough for now I would like to access these properties let's console. log if I need one of these properties take the object you're referring to in this case person dot the name of the property let's do first name person. first name property that would give me SpongeBob let's do this with last name take the object name dot the name of the property person. last name is SquarePants let's do this with age person. AG is 30 person do is employed is set to true all right let's create another object we'll create an object for Patrick const person now objects can't have the same name objects need different names for example I can't have two person objects they can't have the same name syntax error identifier person has already been declared I will rename our first person as person one our second person will be person two then any place in which I'm referencing person will now need to be person one let's reuse these same key value pairs person two's first name will be Patrick his last last name will be star Patrick will be I don't know how old he is according to the TV series let's say he's 42 is Patrick employed no he just watches TV all day now I would like to access person 2's properties okay well console.log person 2's first name Person 2's last name Person two is age person two is employed okay for person two we have Patrick star 42 false so those are properties of an object they key value pairs add as many as you would like in this example these properties mimic real world attributes that a person might have like a first name last name age whatever you can think of but not only that objects can have dedicated functions that we refer to as methods what sorts of actions can these objects perform for example a person could eat they could drink they could sleep what sorts of things do people do so just to keep this simple I am going to create a function to say hello we'll start with person one say hello colon space this will be a function expression what are we going to do when we invoke this function let's console.log what would SpongeBob say if he's greeting somebody hi I'm SpongeBob be sure you're not including a semicolon at the end that should work let's test it I'm going to have person one invoke their say hello method hi I'm SpongeBob let's add a say hello method to Patrick person two we'll change the output of say hello what would Patrick say hey I'm Patrick dot dot dot now we'll have person two use their say hello method hey I'm Patrick you can add as many methods as you would like so with these functions we're using a function expression if you prefer you could even use an arrow function list your arguments Arrow then the code you would like to perform this would work too let's add an eat method eat function we'll use a function expression for SpongeBob let's console.log what is SpongeBob eating I am eating a crabby patty we'll include a neat function for Patrick as well what's Patrick going to eat Patrick is eating I am eating roast beef chicken and pizza and if you would like you can use an arrow function for this example list your parameters Arrow then the code you would like to perform it's not necessary but I do like Arrow functions person one SpongeBob will use his eat method same thing goes with Patrick person two so SpongeBob is eating a crabby patty Patrick is eating roast beef chicken and Pizza all right everybody so JavaScript objects are a collection of related properties and or methods properties are what an object has methods are functions that an object can perform they can represent Real World objects such as people products or places properties are key value pairs methods are functions that belong to an object and well everybody that is an introduction to object-oriented programming in JavaScript
Info
Channel: Bro Code
Views: 26,383
Rating: undefined out of 5
Keywords: javascript, javascript tutorial, javascript full course, javascript course, course, full course, web development, web developer, software engineer, software engineering, coding tutorial, learn to code, tutorial, html, css, objects
Id: lo7o91qLzxc
Channel Id: undefined
Length: 7min 0sec (420 seconds)
Published: Tue Nov 14 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.