Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:02,220 --> 00:00:08,790
We learned about imports and exports. Another core feature of next generation javascript are classes
2
00:00:09,390 --> 00:00:14,190
if you're coming from some other programming language, you might already know classes.
3
00:00:14,250 --> 00:00:17,880
Classes are essentially blueprints for objects.
4
00:00:17,880 --> 00:00:24,390
In our case here for javascript objects. A class is created with the class keyword and a class can have
5
00:00:24,390 --> 00:00:27,860
both properties and methods.
6
00:00:28,110 --> 00:00:33,720
Methods are simply functions attached to classes where properties are variables attached to classes.
7
00:00:33,720 --> 00:00:39,440
You could say we'll use both in this course and then we'll obviously always explain what we're doing here.
8
00:00:39,480 --> 00:00:46,850
When we add such a property or method a class is instantiated like this with the new keyword.
9
00:00:46,980 --> 00:00:50,790
And this might of course look familiar to you if you worked a bit with javascript.
10
00:00:50,850 --> 00:00:56,190
You might notice from constructor functions and indeed classes are kind of a more convenient way of
11
00:00:56,190 --> 00:01:02,520
creating constructor functions so you create javascript objects with classes as blueprints.
12
00:01:02,520 --> 00:01:06,290
That's the idea and classes also support inheritance.
13
00:01:06,300 --> 00:01:12,030
Which means you have another class which you inherit from taking all its properties and methods and
14
00:01:12,030 --> 00:01:15,140
potentially adding new properties and methods.
15
00:01:15,350 --> 00:01:17,110
That also might look familiar to you.
16
00:01:17,250 --> 00:01:19,450
You might notice from prototypes.
17
00:01:19,470 --> 00:01:21,860
Let's have a look at classes in action.
18
00:01:22,260 --> 00:01:24,160
Let's create a new class here.
19
00:01:24,270 --> 00:01:27,310
I'll name it person just like this.
20
00:01:27,330 --> 00:01:30,570
Then you have curly braces to mark the class body.
21
00:01:30,690 --> 00:01:38,070
And now there we can start using properties now in its simplest form a property is added by adding a
22
00:01:38,070 --> 00:01:41,760
constructor that is a default function method.
23
00:01:41,820 --> 00:01:47,490
You can add to any class which will be executed whenever you instantiate the class and the method is
24
00:01:47,490 --> 00:01:52,470
created just with the name of the method parentheses and then curly braces.
25
00:01:52,860 --> 00:01:59,070
And then there we can now set up properties with the this keyword and we could write this name equals max
26
00:01:59,070 --> 00:02:00,290
for example.
27
00:02:00,750 --> 00:02:02,590
We can now also add a method.
28
00:02:02,880 --> 00:02:12,720
PrintMyName and there we could simply output this.name referring to the name property we created.
29
00:02:12,720 --> 00:02:20,250
Now we can use this class to store an instance in a constant with new person and then we can execute
30
00:02:20,260 --> 00:02:21,510
person.
31
00:02:21,510 --> 00:02:23,870
PrintMyName.
32
00:02:24,150 --> 00:02:27,780
If we now hit run we see Max.
33
00:02:27,950 --> 00:02:30,930
This is how easy we can use a class.
34
00:02:30,960 --> 00:02:33,420
Now I said that classes can also inherit.
35
00:02:33,450 --> 00:02:43,620
So what we can do is we can create another class human, and there we might also add a constructor to
36
00:02:43,620 --> 00:02:51,060
set this.gender equal to male or female of course if you want, what you feel, whatever you want and you could
37
00:02:51,060 --> 00:02:55,680
at printGender and then output console log.
38
00:02:55,770 --> 00:02:57,840
this.gender.
39
00:02:58,180 --> 00:03:04,420
And now if person extends then that's a keyword word again extends human.
40
00:03:04,660 --> 00:03:12,280
Now we inherit this property and this method printGender and we can use both on the person as well
41
00:03:12,730 --> 00:03:17,390
so we can also call a person.printGender like that.
42
00:03:17,390 --> 00:03:21,720
However before we run to successfully we're actually let's try it.
43
00:03:21,820 --> 00:03:28,480
We'll get an error that we must call the super constructor in the derived class and that's important.
44
00:03:28,630 --> 00:03:34,320
If you are extending another class and you are implementing the constructor which you don't have to.
45
00:03:34,330 --> 00:03:41,280
But if you are then you have to add super this special super method in the constructor.
46
00:03:41,320 --> 00:03:47,470
It's a keyword and it simply executes the parent constructor to which you of course have to to correct
47
00:03:47,470 --> 00:03:49,540
to initialize the parent class.
48
00:03:49,570 --> 00:03:56,340
So now you hit clear and run you'll see Max and male and obviously you could now go into your person
49
00:03:56,350 --> 00:04:02,740
class and still set gender there to female which is not 100 percent correct here but that's just to show case
50
00:04:02,740 --> 00:04:03,880
that this works.
51
00:04:03,940 --> 00:04:08,960
Now you see we print female even though we're still calling printGender here.
52
00:04:09,070 --> 00:04:17,800
But it is extended by person so these are classes and classes are used by React to create its components.
53
00:04:17,800 --> 00:04:20,959
At least this is one of the two ways of creating components.
54
00:04:20,980 --> 00:04:26,500
This is how you will see me use it in this course and it's important to understand that classes are
55
00:04:26,500 --> 00:04:33,850
just blueprints for javascript objects and are very comparable to constructor functions where inheritance
56
00:04:33,880 --> 00:04:35,640
is comparable to prototypes.
5993
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.