Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,170 --> 00:00:08,710
So I mentioned this concept of using classes and we haven't worked with classes at all in this course yet.
2
00:00:09,400 --> 00:00:17,650
Classes allow us to build objects in an easier way or to build objects based on some blueprint to be
3
00:00:17,650 --> 00:00:18,280
precise.
4
00:00:18,370 --> 00:00:20,740
So we have objects and we have classes
5
00:00:20,740 --> 00:00:26,310
and the idea is that objects are the things we work with in code and we worked with objects a lot in
6
00:00:26,310 --> 00:00:27,440
this course already.
7
00:00:27,580 --> 00:00:33,490
Now classes are something we can create in Javascript too, which do not replace objects but instead which
8
00:00:33,490 --> 00:00:40,960
allow us to define blueprints for objects so that we can easily recreate objects based on these classes
9
00:00:40,960 --> 00:00:48,280
because indeed objects then are also called instances of classes. So we can create an object based on
10
00:00:48,280 --> 00:00:53,860
some class thereafter and therefore a class is just a definition of how the object looks like, which
11
00:00:53,860 --> 00:00:55,930
properties and methods it has,
12
00:00:56,140 --> 00:01:02,230
the place where we store our logic and then the object is the concrete thing we build based on that
13
00:01:02,230 --> 00:01:09,660
class with which we work in our code. So this class based creation which I'll show you in this module
14
00:01:09,990 --> 00:01:12,550
is an alternative to using object literals.
15
00:01:12,630 --> 00:01:19,110
It's not always better, object literal notation is great if you will just want to quickly group some
16
00:01:19,110 --> 00:01:25,920
data together but it's awesome to use classes if you have reusable logic which you wanted to define in
17
00:01:25,920 --> 00:01:29,230
one place and then create multiple objects based on it.
18
00:01:29,310 --> 00:01:35,580
So classes can make the creation of multiple, similar looking objects where the data differs but not
19
00:01:35,580 --> 00:01:43,360
the structure much easier. So let's translate this code which I wrote here into a class-based system
20
00:01:43,360 --> 00:01:48,460
and let's start with this object in this array. This object always looks the same and as I mentioned,
21
00:01:48,460 --> 00:01:54,650
we could create a function that builds us such an object, well a class in the end is such a function,
22
00:01:54,670 --> 00:02:00,670
it's just a different more powerful way or an easier way of writing such a function.
23
00:02:00,670 --> 00:02:04,720
You add a class in Javascript by adding the class keyword just like this,
24
00:02:04,720 --> 00:02:06,640
it's a reserved keyword
25
00:02:06,640 --> 00:02:08,530
and then any name of your class,
26
00:02:08,590 --> 00:02:12,360
the convention is to start that name with a capital character,
27
00:02:12,370 --> 00:02:19,060
so here for example Product, like that. If you have a word that consists of multiple words like product
28
00:02:19,180 --> 00:02:24,820
item, you would write it like this every sub word starts with a new capital character,
29
00:02:24,820 --> 00:02:30,310
there must not be any whitespace or dashes in there, that would all be syntax errors.
30
00:02:30,310 --> 00:02:34,870
You could theoretically also name your class like that starting with a lower case character but the
31
00:02:34,870 --> 00:02:37,960
convention is to start with an uppercase character.
32
00:02:38,020 --> 00:02:44,020
So now I got my product class here and then you add curly braces to define what's inside of that class
33
00:02:44,050 --> 00:02:51,280
and what's inside of that class is basically your blueprint of how an object created
34
00:02:51,280 --> 00:02:58,000
based on that class should look like. So you define which properties such an object should have and which
35
00:02:58,000 --> 00:02:59,700
methods it should have.
36
00:02:59,890 --> 00:03:03,520
Now for that, you go into your class here and there,
37
00:03:03,520 --> 00:03:10,210
we can add a property or here we would call it a field and I'll come back to the difference in a second,
38
00:03:10,210 --> 00:03:16,600
it's not that big, by simply adding it in here, for example title and then you can assign a default value
39
00:03:16,600 --> 00:03:20,470
here because keep in mind the product class will always act as a blueprint,
40
00:03:20,470 --> 00:03:25,090
that's why I'm saying default because typically you'll assign a new value but we can create a default
41
00:03:25,090 --> 00:03:27,500
value of let's say default here.
42
00:03:27,520 --> 00:03:32,830
Now one important note which you can see right away, inside of this class definition, you assign values
43
00:03:32,830 --> 00:03:38,410
with an equal sign, not with a colon which you would do an object literal but with an equal sign and
44
00:03:38,410 --> 00:03:44,560
you end a line after such a field with a semicolon, not with a comma.
45
00:03:44,560 --> 00:03:45,990
That's one important difference
46
00:03:46,030 --> 00:03:49,500
and classes do not replace objects as you will see, instead
47
00:03:49,540 --> 00:03:56,380
as I mentioned multiple times, we build objects based on classes. So we might have a title, an imageUrl,
48
00:03:56,380 --> 00:04:01,040
which could be an empty string or you just don't set any value at all,
49
00:04:01,110 --> 00:04:07,300
then it will be undefined initially and also a description and also a price.
50
00:04:07,320 --> 00:04:10,140
Now this would defined that objects based on product
51
00:04:10,140 --> 00:04:12,960
will have these four properties thereafter.
52
00:04:12,960 --> 00:04:14,860
Now I mentioned, I call in fields here
53
00:04:15,030 --> 00:04:20,760
once we create an object, based on such a class every field will be translated to a property in that
54
00:04:20,760 --> 00:04:21,510
object,
55
00:04:21,510 --> 00:04:22,680
that's how that's related,
56
00:04:22,680 --> 00:04:27,420
we just call it field here because the class is not the concrete thing with which we work here,
57
00:04:27,660 --> 00:04:29,680
instead it's just a blueprint,
58
00:04:29,700 --> 00:04:35,850
it's just a definition of an object and what becomes a property in an object is just called a field
59
00:04:35,850 --> 00:04:36,220
here,
60
00:04:36,480 --> 00:04:42,110
however for the rest of this course, I'll also use these terms interchangeably because in the end they're
61
00:04:42,120 --> 00:04:43,800
closely related.
62
00:04:43,830 --> 00:04:46,260
So now that's how a product would look like.
63
00:04:46,290 --> 00:04:51,570
Now the question is, how do you now create an object based on such a class? Well you go to the place where
64
00:04:51,570 --> 00:04:52,360
you need it,
65
00:04:52,410 --> 00:04:59,370
that could be here in your global script or here in this array of products and then you refer to the
66
00:04:59,370 --> 00:05:06,780
class name, product, you execute it like a function and very important, you add a keyword in front of it,
67
00:05:07,110 --> 00:05:11,630
new and we've seen this new keyword before in this course,
68
00:05:11,760 --> 00:05:18,450
I sometimes showed it. New in the end is a keyword Javascript understands that together with such a
69
00:05:19,200 --> 00:05:25,980
function execution which is based on a class, it's not a real function but still, should basically create
70
00:05:26,040 --> 00:05:26,790
a new object,
71
00:05:26,790 --> 00:05:31,320
that's what new together with this function execution does. Neither of the two alone works,
72
00:05:31,320 --> 00:05:32,800
we have to combine them.
73
00:05:32,880 --> 00:05:36,210
So now this gives us a new product object,
74
00:05:36,240 --> 00:05:44,190
this returns a new object which has this structure and therefore indeed if we comment this out for a second
75
00:05:44,190 --> 00:05:50,160
and we just go here and we console log new product to have a look at how this looks like, such an object,
76
00:05:50,730 --> 00:05:51,660
let's save that.
77
00:05:51,750 --> 00:05:57,240
Since I added this globally, it should execute right away when a script runs and then reload this page,
78
00:05:57,250 --> 00:06:02,550
also make sure you're using Chrome because classes are not supported in old versions of Internet Explorer
79
00:06:02,550 --> 00:06:08,310
for example and I'll come back how to still make them work there in the browser support module. So reload
80
00:06:08,310 --> 00:06:13,650
your page thereafter and what you should see is this object which is getting logged here which has a
81
00:06:13,650 --> 00:06:16,100
title property with a value of default,
82
00:06:16,110 --> 00:06:19,280
please note that this is a regular object with a regular property
83
00:06:19,290 --> 00:06:25,050
hence we have a colon separated key-value pair and then we have the three other properties which are part
84
00:06:25,050 --> 00:06:29,400
of the object but which have no value because we haven't assigned one here.
85
00:06:29,790 --> 00:06:33,660
So that's the first step towards classes, we now know how to define one
86
00:06:33,690 --> 00:06:39,990
but it's not that useful yet. When we create a product based on such a class, it would be nice if we could
87
00:06:39,990 --> 00:06:45,730
create it with some initial values. We could of course assign all values in here
88
00:06:45,780 --> 00:06:51,070
but then all products which we create based on the product would have the same values.
89
00:06:51,120 --> 00:06:57,210
Instead I want to make sure that when we call new product, we can kind of pass our initial values here
90
00:06:57,480 --> 00:07:02,030
to new product so that we can create a new object with some initial values.
91
00:07:02,190 --> 00:07:08,550
Of course by the way, we could of course create a new product like this and then just assign values with
92
00:07:08,550 --> 00:07:12,930
the dot notation but that's not what we want to do, we want to create it in one go,
93
00:07:12,960 --> 00:07:14,910
so let's have a look at how that works in a second.
10522
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.