Object Properties
Its time for us to create our first object properties and then use them. we will learn what the difference between variables and properties are.
Video Transcript
00:00All right, so let's move on.
00:02So now that we got that piece out of the way,
00:05I wanna just comment this whole block out
00:07and I want us to continue.
00:09So I'm just gonna move that all the way down there
00:13and let's start with working
00:15with the base base object type.
00:17So so far we worked with the date object type
00:19and we wanna create the regular object.
00:21So I'm just gonna call our variable object
00:23and again, I'm gonna say new
00:24and then the type of whatever it is.
00:26And again, it's really important the way it's written,
00:30classes in general start with an uppercase
00:32and it's just cause that's how the blueprint is called.
00:34If you call it with a lowercase,
00:36you'll just not call that class.
00:38All right, so we wanna create a new object
00:41and the object is really the most primitive basic
00:44and I shouldn't use the word primitive
00:46so not to confuse with primitive variables
00:48but it's the most basic object type.
00:51Every single other object is derived out of that object.
00:54And although we didn't talk about
00:56object oriented programming yet,
00:58this is really the first steps
01:00into object oriented programming
01:02where until a couple of videos ago,
01:04an hour ago, we were working with loose ended things.
01:07We're working with variables,
01:08we're working with functions
01:10but we didn't have anything
01:11that tied it all together into clusters.
01:14And that's exactly where objects come into the picture.
01:16There's much more beyond that
01:17but that's a big component of why we work with objects
01:20is to group things thematically
01:22based on a logical breakdown
01:25such as our math class that we met,
01:27such as the date object that we've met.
01:29All right, so the new object when we created,
01:31there are fruit properties that we could access
01:33but they're really not interesting.
01:35They're not something you would ever really need to approach
01:38unless you're an advanced developer
01:39because it's a very low end type of things
01:41that the default of the most basic thing that object has,
01:44every single object has
01:46because a date is a type of object.
01:49It's not only a beautiful thing to say
01:50that it's a type of object
01:52and actually a date extends the object.
01:54It's kind of like the grandchild of that object.
01:57So everything that the object could do,
01:59the date could do and then the date could do more stuff.
02:02But what we could do with the object
02:03is we could create new properties with it.
02:05And the way to do it is really simple
02:07and we've met the dot syntax before briefly.
02:10The dot syntax just as when we wanna access things,
02:13we type a dot and then we press on
02:15whatever we wanna access.
02:16Same is true when we wanna create new things in JavaScript.
02:19So for example, if I wanted to create a property
02:21inside of this new object that I created,
02:23let's say I wanted to call it name.
02:24I could just set that property and just put a value there.
02:28My name is Ben Fallah.
02:29And I could put an object there, let's say school
02:33and the school is zero2geek.com.
02:35You know, our school name changed a couple of times
02:38but right now it's zero2geek.com.
02:40So if you go, no, I'm kidding.
02:42I think this is gonna be our final name.
02:44So with zero2geek.com is our school
02:47and let's create just another variable
02:49and let's say how old or age, right?
02:52So not my age, let's put the age of the school.
02:54The age of the school is two years and we got this.
02:56Let's write 24 for months because it just sounds better.
03:00So we exist 24 months already, that's impressive, long time.
03:04All right, so that was properties, very easy.
03:06We could have put into our properties primitives
03:10as we can see now
03:11but we don't have to put only primitives.
03:12We could also put other advanced objects.
03:15So for example, let's say I wanna put here also
03:17the signature of the date when we created this object.
03:22I could go there and type object and let's say date
03:24and just set there a new date.
03:27All that was good.
03:28So I just created now four different properties.
03:30Now we call them properties
03:31even though there are types of variables, right?
03:33But when a variable belongs to someone,
03:35to who does the name belong?
03:36It belongs to the object that we just created.
03:39To who did the date belong
03:40to the object that we created.
03:41So whenever we create something
03:42that has a relationship to something,
03:46we call it a property.
03:48It's not wrong and it's not a mistake
03:49for you to call it a variable
03:50but that's really the differentiation.
03:52A variable on its own is not a property
03:55but a property is a type of variable, okay?
03:59So it's kinda saying like when you have a variable
04:00that doesn't belong to something, it's just a variable.
04:03When the variable belongs to an object, it's a property.
04:05Okay, so that's an important differentiation
04:07when you're reading different scripts online
04:09to understand why is it called a property,
04:11why is it called a variable and when.
04:14Now that we created all this stuff,
04:15let's see how we access it.
04:17So obviously we could just access it
04:19with our famous alert that we've been using so far
04:22and go to our object name
04:24and we could concatenate a story
04:26and say Benfala works and plays at object.school
04:35and we could run this and when we run it,
04:38we will see the connection of Benfala works
04:41and plays at zero2geeky.com, beautiful.
04:44And we could add into this maybe also
04:46and the time now is, although it's gonna give us
04:52the whole date, which is fine,
04:53unless we say the exact thing that we want.
04:56So let's say that we're gonna say the get time,
04:59which is gonna give us the millisecond since 1970.
05:02So the time now is this really randomly looking number
05:06which is the amount of milliseconds
05:07that passed since the first of the first 1970, cool.
