1
00:00:00,810 --> 00:00:04,180
All right guys so in this video we're going to talk about object literals.

2
00:00:04,200 --> 00:00:09,150
OK I don't want to get too much into object oriented programming yet so we're not going to talk about

3
00:00:09,150 --> 00:00:13,080
constructors or prototypes or anything like that just yet.

4
00:00:13,200 --> 00:00:18,180
But I do want to talk about just the object literal reference type.

5
00:00:18,190 --> 00:00:24,150
So what we're going to do here is create a variable called Person and we're going to set it to a set

6
00:00:24,150 --> 00:00:25,500
of curly braces.

7
00:00:25,560 --> 00:00:27,750
This defines an object literal.

8
00:00:27,840 --> 00:00:30,970
So and here we can put key value pairs.

9
00:00:30,990 --> 00:00:37,220
So let's do first name and then we want this to be a string of Steve.

10
00:00:37,230 --> 00:00:37,580
All right.

11
00:00:37,590 --> 00:00:40,860
Now we want to access this property.

12
00:00:41,010 --> 00:00:44,150
Then we can go console dog log.

13
00:00:44,600 --> 00:00:51,510
You know what we'll do is we'll set just like we've been doing we'll set let thou and we'll save Val

14
00:00:51,540 --> 00:00:58,210
equals person and then down here all console dog log.

15
00:00:58,440 --> 00:01:02,910
The reason I do it like this is so that we don't have Konsole dot logs all over the place we're just

16
00:01:02,970 --> 00:01:08,120
basically mutating Val so Val person let's save that.

17
00:01:08,130 --> 00:01:12,830
And you see we're just going to get a print out of the object itself.

18
00:01:12,840 --> 00:01:15,350
Now if we want to get a specific value

19
00:01:19,850 --> 00:01:30,550
then we can say let's do that now equals person Daut first name and save and we get Steve.

20
00:01:30,620 --> 00:01:31,420
All right.

21
00:01:31,420 --> 00:01:36,260
Now that's not the only way that we can select values from an object.

22
00:01:36,290 --> 00:01:38,780
We can also use this syntax.

23
00:01:39,110 --> 00:01:44,510
We can say person and then use some brackets and say first name

24
00:01:47,240 --> 00:01:49,160
save that and we get Steve.

25
00:01:49,160 --> 00:01:49,520
All right.

26
00:01:49,520 --> 00:01:53,240
Now this is the way that I recommend this is the way that you're usually going to see.

27
00:01:53,760 --> 00:01:56,270
But you can do it this way.

28
00:01:56,270 --> 00:02:02,380
All right so let's add some other stuff we'll say last name Smith.

29
00:02:02,690 --> 00:02:08,750
So if we were to change this to last name you know c we get Smith.

30
00:02:09,050 --> 00:02:09,400
All right.

31
00:02:09,410 --> 00:02:16,890
So those are strings we can put numbers so we'll say age 30 let's do email

32
00:02:19,720 --> 00:02:21,660
let's say Steve.

33
00:02:21,900 --> 00:02:26,890
He's old school so he still has an aol dot com email address.

34
00:02:26,970 --> 00:02:29,720
I would say hobbies.

35
00:02:30,750 --> 00:02:32,620
So we'll put an array in here.

36
00:02:33,000 --> 00:02:34,140
Let's say music

37
00:02:36,470 --> 00:02:38,950
sports.

38
00:02:39,030 --> 00:02:39,380
All right.

39
00:02:39,380 --> 00:02:46,200
So we can put a raise we can put embedded objects so basically another object so will do address so

40
00:02:46,410 --> 00:02:48,280
that system curly braces.

41
00:02:48,340 --> 00:02:49,350
Let's say City

42
00:02:51,930 --> 00:03:00,870
Miami and let's say state Florida OK we can also do functions.

43
00:03:00,870 --> 00:03:08,880
So let's say get birthday or let's to get birth year

44
00:03:11,730 --> 00:03:18,050
and we'll set that to a function and will return.

45
00:03:18,050 --> 00:03:19,250
I don't know the exact year.

46
00:03:19,250 --> 00:03:28,250
I'm just going to say 1980 in 0 7 and I'm going to change is going to show you something that we can

47
00:03:28,250 --> 00:03:28,900
do in a minute.

48
00:03:28,910 --> 00:03:31,040
But for now I'm just going to return the value.

49
00:03:31,280 --> 00:03:36,530
So if we go out here we can a vowel equals person Datsik age.

50
00:03:36,800 --> 00:03:47,480
OK that will give us 30 we can say the person thought would we have let's do the array so hobbies and

51
00:03:47,480 --> 00:03:53,960
that will print out the whole array if we want just let's say sports that's in the one position arrays

52
00:03:53,960 --> 00:03:57,860
are zero base which we learned in the last section in The Last Lecture.

53
00:03:57,860 --> 00:04:04,340
So let's say Hobbes and then open up some brackets and we'll put in a one and that's going to give us

54
00:04:04,340 --> 00:04:06,020
sports.

55
00:04:06,020 --> 00:04:06,500
All right.

56
00:04:06,500 --> 00:04:15,380
Now for the Address object we'll say person Daut address that will actually print out the whole object.

57
00:04:15,380 --> 00:04:19,030
But let's say we just wanted to state we could say Dotts state.

58
00:04:19,460 --> 00:04:19,850
All right.

59
00:04:19,850 --> 00:04:25,440
And then of course we could also do whoops we could also do the other syntax.

60
00:04:26,780 --> 00:04:33,990
So if we wanted to do brackets and then city save that that will also work.

61
00:04:34,000 --> 00:04:35,540
All right so for the function

62
00:04:38,180 --> 00:04:46,640
say person Daut get birth year we need to have some parentheses because it is a function save that we

63
00:04:46,640 --> 00:04:48,530
get 1987.

64
00:04:48,770 --> 00:04:49,460
All right.

65
00:04:49,490 --> 00:04:56,070
Now within objects we can access other properties.

66
00:04:56,090 --> 00:05:03,920
Right so let's say for the birth year we want to calculate this so we're going to return 2017 which

67
00:05:03,920 --> 00:05:08,230
is the current year and we want to take away the age.

68
00:05:08,270 --> 00:05:17,390
OK the year the whatever is in the age now to access this if we just say age and we save that we're

69
00:05:17,390 --> 00:05:23,120
going to get this Arutz is age is not defined when you're inside an object like this you need to use

70
00:05:23,120 --> 00:05:24,690
the this keyword.

71
00:05:24,880 --> 00:05:25,270
OK.

72
00:05:25,280 --> 00:05:29,110
So this is pertaining to the object.

73
00:05:29,150 --> 00:05:34,440
Now we're going to get more into this later on but I just wanted to show you a basic example.

74
00:05:34,460 --> 00:05:41,760
So if we go ahead and save that now we'll get 1987 it's actually calculating it by subtracting whatever

75
00:05:41,760 --> 00:05:44,190
is in the age from 2017.

76
00:05:44,330 --> 00:05:51,490
So if I change the age to let's say 36 and save we get 1981.

77
00:05:51,720 --> 00:05:52,150
All right.

78
00:05:52,150 --> 00:05:57,010
So another thing that you'll often see is arrays of objects.

79
00:05:57,010 --> 00:06:04,660
So for instance if we have an array called people look at that to a set of brackets and then and here

80
00:06:04,660 --> 00:06:07,090
we can have different objects so name

81
00:06:12,710 --> 00:06:21,710
and then we can put a comma here and we could have another object.

82
00:06:22,150 --> 00:06:22,750
All right.

83
00:06:22,750 --> 00:06:26,370
And then we could actually loop through this if we wanted to.

84
00:06:26,380 --> 00:06:28,560
For instance we could do a for loop.

85
00:06:28,570 --> 00:06:33,130
Now I haven't gotten into loops yet so don't worry about it but just to show you we can do.

86
00:06:33,400 --> 00:06:35,570
I would say let's.

87
00:06:35,830 --> 00:06:37,100
Equals zero.

88
00:06:37,330 --> 00:06:42,050
As long as it is less than people dot length.

89
00:06:42,200 --> 00:06:50,050
So as long as it's less than the length of the array and then we just want to increment by 1 through

90
00:06:50,050 --> 00:06:58,040
every iteration and then let's just do a console log of people.

91
00:06:58,780 --> 00:07:01,250
The current iteration which would be I.

92
00:07:01,300 --> 00:07:03,160
And then let's go let's get the names.

93
00:07:03,160 --> 00:07:06,520
So we'll say don't name and save and now it's going to print out.

94
00:07:06,520 --> 00:07:11,630
John and Mike if I were to add another object here.

95
00:07:12,940 --> 00:07:16,740
So let's call this one Nancy.

96
00:07:16,920 --> 00:07:21,270
Let's say she's 40 and save and you can see that now Nancy is getting added.

97
00:07:21,340 --> 00:07:25,990
But don't worry about it if you don't know anything about loops we're going to get into that later on.

98
00:07:25,990 --> 00:07:32,110
All right so that's I mean that's the core fundamentals of creating an object literal will get into

99
00:07:32,110 --> 00:07:40,890
constructors and things like that later which is another way to create objects and object instances.

100
00:07:40,890 --> 00:07:41,210
All right.

101
00:07:41,220 --> 00:07:42,330
So thanks guys.

102
00:07:42,330 --> 00:07:48,540
In the next video we're going to take a look at Dayton times so working with dates and times and Javascript.

