Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,040 --> 00:00:04,170
So now that we learned about functions,
2
00:00:04,170 --> 00:00:07,170
I want to come back to values.
3
00:00:07,170 --> 00:00:11,130
And specifically, I want to come back to objects.
4
00:00:11,130 --> 00:00:13,380
As you should already know,
5
00:00:13,380 --> 00:00:14,730
objects in JavaScript
6
00:00:14,730 --> 00:00:18,690
can be used to group multiple values together.
7
00:00:18,690 --> 00:00:23,690
So for example, if I have a user name, which could be Max,
8
00:00:24,570 --> 00:00:27,030
which typically of course would not be hard coded,
9
00:00:27,030 --> 00:00:29,160
but might be fetched from a server
10
00:00:29,160 --> 00:00:32,313
or from some HTML input on the page,
11
00:00:33,150 --> 00:00:37,500
and I have a user age, let's say, of 34.
12
00:00:37,500 --> 00:00:41,490
I could group these values together in an object
13
00:00:41,490 --> 00:00:43,710
so that instead of defining them like this,
14
00:00:43,710 --> 00:00:45,960
I could have a user object,
15
00:00:45,960 --> 00:00:47,430
which is created like this
16
00:00:47,430 --> 00:00:49,740
by using opening and closing curly braces
17
00:00:49,740 --> 00:00:52,920
on the right side of this equal sign here, like this.
18
00:00:52,920 --> 00:00:56,250
And then there I could have a name property, as it's called,
19
00:00:56,250 --> 00:00:57,750
and a age property.
20
00:00:57,750 --> 00:01:00,210
So I have key value pairs
21
00:01:00,210 --> 00:01:02,310
where the keys are up to you,
22
00:01:02,310 --> 00:01:04,080
but should follow the naming rules
23
00:01:04,080 --> 00:01:05,550
you already learned about.
24
00:01:05,550 --> 00:01:08,340
They also should, of course, be descriptive.
25
00:01:08,340 --> 00:01:10,590
And then separated by a colon,
26
00:01:10,590 --> 00:01:14,520
I have the value that should be stored under that key.
27
00:01:14,520 --> 00:01:18,000
Now, this object could be used to output it
28
00:01:18,000 --> 00:01:19,743
in the console, for example.
29
00:01:20,640 --> 00:01:24,570
If I refresh this page here, I would see my user.
30
00:01:24,570 --> 00:01:26,250
And in this JavaScript console,
31
00:01:26,250 --> 00:01:27,873
I can actually also explore it,
32
00:01:29,430 --> 00:01:31,410
but I can also access
33
00:01:31,410 --> 00:01:34,500
individual fields of that user object here,
34
00:01:34,500 --> 00:01:35,670
or of any object,
35
00:01:35,670 --> 00:01:37,590
by using the dot notation.
36
00:01:37,590 --> 00:01:40,260
And then for example, I can just access and output,
37
00:01:40,260 --> 00:01:42,930
in this case, the name field.
38
00:01:42,930 --> 00:01:45,990
So there, for now, if I reload this to get rid of the error,
39
00:01:45,990 --> 00:01:47,427
I would see "Max."
40
00:01:48,270 --> 00:01:51,150
And this dot here is the key
41
00:01:51,150 --> 00:01:53,550
to accessing values in an object.
42
00:01:53,550 --> 00:01:56,220
You use this dot notation to get access
43
00:01:56,220 --> 00:01:58,383
to a value in an object.
44
00:01:59,250 --> 00:02:01,500
Now, besides key value pairs like this,
45
00:02:01,500 --> 00:02:04,230
objects can also store functions,
46
00:02:04,230 --> 00:02:07,320
which we then typically call methods.
47
00:02:07,320 --> 00:02:11,760
For example, here I could create a greet function like this
48
00:02:11,760 --> 00:02:13,830
without the function keyword,
49
00:02:13,830 --> 00:02:16,260
just the name of the function you want to have,
50
00:02:16,260 --> 00:02:18,420
followed by the parentheses,
51
00:02:18,420 --> 00:02:21,870
which could accept parameters if you wanted to,
52
00:02:21,870 --> 00:02:23,820
And then your curly braces.
53
00:02:23,820 --> 00:02:25,770
And here you can return something,
54
00:02:25,770 --> 00:02:29,343
or for example, simply console log something.
55
00:02:30,930 --> 00:02:33,450
And then you could access this,
56
00:02:33,450 --> 00:02:35,070
again with the dot notation,
57
00:02:35,070 --> 00:02:37,080
and execute it like any other function
58
00:02:37,080 --> 00:02:38,853
by adding these parentheses here.
59
00:02:39,960 --> 00:02:41,640
With that, if I reload this,
60
00:02:41,640 --> 00:02:43,443
I would see "Hello!" here.
61
00:02:44,550 --> 00:02:47,040
Now, if you are in such a method,
62
00:02:47,040 --> 00:02:49,650
so in a function that belongs to an object,
63
00:02:49,650 --> 00:02:53,700
you can also access the properties of this object,
64
00:02:53,700 --> 00:02:56,070
so the other methods and fields
65
00:02:56,070 --> 00:02:57,570
that belong to this object,
66
00:02:57,570 --> 00:02:59,733
with help of the special "this" keyword.
67
00:03:00,660 --> 00:03:01,680
This would, for example,
68
00:03:01,680 --> 00:03:03,630
allow me to output the age here
69
00:03:03,630 --> 00:03:08,433
by using this, which refers to this object, .age.
70
00:03:09,570 --> 00:03:11,880
And with that, if I reload this,
71
00:03:11,880 --> 00:03:14,073
I would output the age here.
72
00:03:15,720 --> 00:03:19,140
The this keyword won't be too important for this course,
73
00:03:19,140 --> 00:03:22,290
because we won't create too many methods here,
74
00:03:22,290 --> 00:03:25,890
but nonetheless, it is a JavaScript concept you should know.
75
00:03:25,890 --> 00:03:28,290
Now, when it comes to creating objects,
76
00:03:28,290 --> 00:03:30,720
you can either create them like this
77
00:03:30,720 --> 00:03:33,570
to group values together,
78
00:03:33,570 --> 00:03:36,180
or you can create blueprints
79
00:03:36,180 --> 00:03:39,000
by using the special class keyword.
80
00:03:39,000 --> 00:03:40,620
This is also an approach
81
00:03:40,620 --> 00:03:42,720
we won't use too much in this course,
82
00:03:42,720 --> 00:03:44,883
but I briefly want to mention it here.
83
00:03:45,810 --> 00:03:47,220
With the class keyword,
84
00:03:47,220 --> 00:03:49,050
you can create a blueprint
85
00:03:49,050 --> 00:03:53,670
that can then later be used to create the actual objects.
86
00:03:53,670 --> 00:03:58,530
Here we could create a blueprint called "User," like this.
87
00:03:58,530 --> 00:04:01,620
And what's important here is that for class names,
88
00:04:01,620 --> 00:04:04,623
you should start with a capital case character.
89
00:04:05,970 --> 00:04:09,000
And then here you could, for example, add your methods
90
00:04:09,000 --> 00:04:13,713
that should be part of that blueprint, like this.
91
00:04:15,180 --> 00:04:18,180
In addition, you can add a so-called constructor function
92
00:04:18,180 --> 00:04:21,149
by using the special constructor keyword,
93
00:04:21,149 --> 00:04:22,079
which other than that
94
00:04:22,079 --> 00:04:24,720
is written like a normal function though,
95
00:04:24,720 --> 00:04:28,560
and you can use this constructor for accepting parameters,
96
00:04:28,560 --> 00:04:31,563
input values, like name and age, for example,
97
00:04:32,520 --> 00:04:35,550
and then store them in properties of the object
98
00:04:35,550 --> 00:04:38,040
that will be created based on the class
99
00:04:38,040 --> 00:04:40,620
with help of the this keyword again.
100
00:04:40,620 --> 00:04:42,780
So that's how I would create properties
101
00:04:42,780 --> 00:04:44,970
named "name" and "age"
102
00:04:44,970 --> 00:04:47,910
and store the parameter values "name" and "age"
103
00:04:47,910 --> 00:04:49,503
in these properties.
104
00:04:50,820 --> 00:04:54,030
Now, by doing that alone, we won't achieve anything yet.
105
00:04:54,030 --> 00:04:57,390
Instead, we can now use such a class blueprint
106
00:04:57,390 --> 00:05:00,840
by instantiating it with help of the special new keyword
107
00:05:00,840 --> 00:05:05,220
which creates a new object based on that blueprint.
108
00:05:05,220 --> 00:05:10,220
And then here I could pass "Manuel" and "35" as values.
109
00:05:11,580 --> 00:05:14,820
And if I now console log user one here,
110
00:05:14,820 --> 00:05:18,750
I would be getting an object, this object here,
111
00:05:18,750 --> 00:05:21,960
that is based on this blueprint.
112
00:05:21,960 --> 00:05:25,680
So this object here is based on this blueprint,
113
00:05:25,680 --> 00:05:28,800
and therefore it contains the values I passed
114
00:05:28,800 --> 00:05:33,030
as input parameters to this constructor here.
115
00:05:33,030 --> 00:05:35,583
That's how these things work together.
116
00:05:38,160 --> 00:05:40,800
In addition, this user one object
117
00:05:40,800 --> 00:05:42,720
would now also have a greet method
118
00:05:42,720 --> 00:05:45,120
because I defined the greet method here.
119
00:05:45,120 --> 00:05:47,943
So that's where this "Hi!" is coming from.
120
00:05:50,730 --> 00:05:53,910
Again, we won't use this class keyword a lot in this course,
121
00:05:53,910 --> 00:05:56,220
but you should know about it.
122
00:05:56,220 --> 00:05:58,800
But with that, that's it for objects.
123
00:05:58,800 --> 00:06:01,683
I'm going to comment out of this code here,
124
00:06:02,820 --> 00:06:05,913
and we can continue with the refresher for the next concept.
9645
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.