Under the hood, classes are special functions. Just like you can define functions using function expressions and declarations, you can define classes as well. One way to define classes is using class declaration.
You can use the class
keyword and the name of the class. This syntax is very similar to that of Java or C#:
class Car { constructor(model, year){ this.model = model; this.year = year; } } console.log(typeof Car); //"function"
To establish the fact that classes are a special function, if we get the typeof
the Car
class, we will get a function.
There is an important distinction between classes and normal functions. While normal functions are hoisted, classes are not. A normal function is available immediately when you enter a scope in which it is declared; this is called hoisting, which means that a normal function can be declared anywhere in the scope, and it will be available. However...