Welcome back to Learn to Code and JavaScript in this lecture, we are going to start talking about objects.
Objects are just a way of grouping data together.
So how do we define an object?
An object is defined very similarly to how in arrays defined so we can group data together into one
package and we use curly braces to define an object literal with commas separating the values.
The difference between an object and an array.
Is that the items in an object have a name.
So this object has a name and age and a signed up attribute.
So what my object equals, and you start with a curly brace name Colin, and of course, a string Bob
comma, the next item, age column 12 comma, signed up colon.
True.
So that defines an object so the object owners can be accessed by their name.
So I can say council that log my object, that name my object, that age.
So I just can also be accessed using a string name with square brackets, kind of like an array, so
I can say my object square bracket, string name, my object, square brackets, string age.
So there's two ways to access the objects.
There's also a null object.
And you may have heard me refer to null.
So that just means an object that has no value, that has nothing in it.
Objects can store any type of data, including other objects, arrays and functions.
When we get to functions, we'll look at that so I can say let my object equals name.
Sue list an array.
Twenty two.
Forty four, sixty six, eighty eight.
And then sub subject some other object.
So let's take a look at some examples of how objects are built in visual studio code.
All right.
So let's build as an example, an employee object so we'll say what the equals.
And if it's a longer name, we always put it on multiple lines.
If it's a longer set of attributes in the object sequence.
A name
Bill.
No, no, we use Kemel case on our names and of an object as well, and we'll say his employee number
is one, two, three, four or five.
I'm just going to be a strength,
let's say, of four time.
True.
Salary for, say, ten thousand and then will create an array called shifts, which just represents
the days and the week.
So we'll start on Sunday.
So we'll say Sunday, zero eight hours on Monday, eight hours on Tuesday, off on Wednesday, Thursday
eight hours, Friday four hours and Saturday, four hours.
And that's good enough for a quick example.
So we'll leave it like that.
And you need a semicolon at the end.
And let's just log it out
so we'll see what it looks like.
So Output's, it's kind of like a race get output, we can have a little triangle and expand it so we
know full time our name, see the sort of the attributes alphabetically and then our shifts, I can
open those up and see what those are.
And then let's just manipulate this a little bit.
So then we'll say give Bill a raise.
Saddam will say that salary.
And so times equals one point of five dollars, going to get a five percent raise.
So let's actually make those salary be an hourly salary.
And so let's just make it ten dollars an hour.
So now let's calculate bills, weekly pay.
So we'll say let total pay equals zero and we'll say for S of we use the four of the loop and say that
shifts
and we will say total pay equals X times dot salary.
And then we'll say cancel that log.
Hey, Bill.
Total pay.
This is not define let us.
So I paid off 40 to.
And still looking out there on the bottom, so Bill gets paid for it, too, but let's say we've given
Bill a raise.
So Bill's salary is ten dollars and 50 cents after his raise.
And then we calculate the pay.
But let's say we were also going to cut Bill's hours.
So say cut Bill's hours now we can't use an envelope to go through and subtract the hours we need,
use a regular for loop, so we'll save for I equals zero.
I was then and that shifts that the length.
My post was.
And then we'll say Abduh shifts, subi times equals are going to boy 10 percent cut point, not a 10
percent cut in hours.
So each one of these days is going to get cut by 10 percent.
So now let's run this and see what we got.
So now Bill is going to be paid thirty seven dollars and 80 cents and I realized an error early on,
we should have been doing it plus equals here all along.
So we've been underpaying poor Bill.
So three hundred and two dollars and 40 cents, so if we go back and just cut that out, this cut just
to see what that was.
So previous three hundred thirty six dollars, so that's a brief introduction into defining and using
objects.