- Overview
- Transcript
7.5 Classes
Classes in JavaScript are a syntactic sugar that makes it easier to use JavaScript’s prototype-based inheritance. In this lesson, we’ll focus on the way to create objects and deal with inheritance.
Key terms:
class
super
1.Introduction2 lessons, 07:42
1.1Introduction02:12
1.2Setup05:30
2.Language Fundamentals8 lessons, 1:00:53
2.1Variables06:33
2.2Data Types11:28
2.3Arithmetic, Assignment, and Comparison Operators10:24
2.4Unary, Logical, Comma, and Spread Operators09:02
2.5Operator Precedence03:50
2.6Reserved Words04:17
2.7Strict Mode04:34
2.8Functions10:45
3.Data Structures5 lessons, 22:52
3.1Arrays04:29
3.2Objects04:30
3.3Sets04:57
3.4Maps04:21
3.5Weak Maps and Weak Sets04:35
4.Controlling Program Execution7 lessons, 37:06
4.1Conditionals07:49
4.2Switch Statements04:41
4.3The For Loop06:39
4.4The `for .. in` Loop05:17
4.5The `for .. of` Loop04:02
4.6Iterators05:03
4.7While Loops03:35
5.Using JavaScript13 lessons, 1:44:36
5.1Working With Strings09:32
5.2Template Literals05:46
5.3Working With Numbers06:57
5.4Working With Arrays12:53
5.5Iterating and Transforming Arrays07:33
5.6Working With the Object Type13:55
5.7Object Literal Extensions06:45
5.8Working With Object Instances06:45
5.9Getters and Setters05:00
5.10Custom Objects11:28
5.11The `Math` API04:54
5.12Working With Dates and Times08:10
5.13The `Array` Constructor04:58
6.Functions8 lessons, 56:07
6.1The `this` Object06:15
6.2Working With Functions10:11
6.3Scope07:37
6.4Arrow Functions06:59
6.5Generator Functions08:13
6.6Closures05:00
6.7Prototypes06:26
6.8Default and Rest Parameters05:26
7.Miscellaneous6 lessons, 52:39
7.1Destructuring Assignments08:09
7.2AJAX08:30
7.3Regular Expressions10:51
7.4More About Regular Expressions08:38
7.5Classes06:48
7.6ES Modules09:43
8.Working With the DOM6 lessons, 37:39
8.1Selecting HTML Elements05:02
8.2Manipulating HTML Elements07:40
8.3DOM Traversal05:25
8.4Adding and Removing Elements04:45
8.5Creating Elements and Other Nodes04:39
8.6DOM Events10:08
9.Web APIs4 lessons, 17:41
9.1The Selector API03:03
9.2Geolocation05:29
9.3Web Storage05:24
9.4Web Workers03:45
10.Asynchronous JavaScript5 lessons, 26:23
10.1Promises09:52
10.2Promise Chaining05:11
10.3The async Keyword03:21
10.4The await Keyword04:04
10.5More About async and await03:55
11.Conclusion1 lesson, 00:43
11.1Conclusion00:43
7.5 Classes
Hi folks, in this lesson we're going to take a look at classes in JavaScript. As we've learned already, JavaScript's inheritance model is based on an object's prototype, and as such is known as prototypal inheritance. With the introduction of classes, quite recently in JavaScript, in ES 2015 Nothing has actually changed. JavaScript still uses prototypal inheritance. Classes are just a syntactic sugar laid on top of the prototypal inheritance model that just makes working with objects JavaScript a bit easier and a bit nicer to write. So, when we were creating our person constructor function earlier one in the course, what we were doing basically is creating a person class and using that constructor to create instances with that classes. Classes just make it a bit easier and nicer to create these kinds of classes. Let's take a look. So classes are defined using the class keyword. So this is a class declaration. And it looks very similar to a function declaration, except there are no parentheses following the class name. One important point to note however is that while function declarations are hoisted to the top of their scope, class declarations are not hoisted. So an arrow will be thrown if a class is used before it gets declared, unlike a function. Another similarity with functions is that we can have class declarations, or class expressions. And just like functions, class expressions can be unnamed, or named. So the body of the class is added between the curly brackets. Setting the constructor for a class is done using the special constructor property. In this case the constructor accepts a name and this name is set as a property of any instance. Objects created using this class. We can also define methods that all instances of the class will inherit. So in this case we've added a method called shout name. So any employee objects that we create using this employee class. Will automatically inherit this shout name method from the classes prototype. This all happens behind the scenes. We don't have to do anything like updating the prototype ourselves manually. JavaScript just takes care of it for us. So let's now use this class and create a new instance. And let's just take a look at the output in the browser. We can see that the bulk object is shouting its name property. We can also add static methods which are available through the object type rather than through instances of the object. To do this, we just prefix the method name with the static keyword. So this static method can't be called on instances, but it can be called via the class itself. Okay, so you can still see that the string has been reversed. Subclassing is also very simple using another new keyword, the extends keyword. So we can extend the employee class with a new boss class. So let's go back and add a new method to the original employee class. And we'll just call this method work. Now, if we want to override this method in a class that extends the employee class, that's actually really, really easy. All we need to do is provide a method of the same name in the extending class and customize the behavior of the method however we want. Let's go ahead and create the new instance of our boss class. And now let's invoke the work method of each of our objects And we can see that the overriding has worked in exactly the way that we would expect overriding a class method to work. Sometimes when we are extending a class, we might want to invoke a method on the base class from inside the extending class, or the subclass. We have access to the base class from within a subclass via the super keyword. So let's say that when a boss works, they also shout their name. Now, the boss class doesn't have its own shoutName method but, it can still invoke it from the base class. So this example is a little bit contrived. And it's just to show you how we can invoke the method of a base class from within a subclass. It's not something we actually need to do, it's just an example to show you how to do it, and that it is possible. Okay, there we go. So if you do need to invoke a method of the base class from a subclass, it's very easy using the super key route. So in this lesson we looked at a relatively new way of defining classes. Which came to JavaScript as part of ES 2015. We saw that we can define classes using class declarations or class expressions. And that we do that using the class keyword. We learned how to create a constructive for the class with the special constructor property and how to add either instance or static methods to the class. We also saw how to create a sub-class which extends a base class using the extends key word. And we saw how to call methods of the base class. From the subclass using the super keyword. In the next lesson we're going to look at one of the greatest features ever added to JavaScript, ES Modules. Thanks for watching.