- [Instructor] If you look at it, in the end,
classes are functions themselves, but because
they inherit how they are constructed because
of their prototype background, there are some
minor differences in between a straight function
and the class.
That might help you determine
when to use one over the other.
The first difference, as mentioned before,
functions are hoisted to the top of your file
even when declared dead last
versus classes where they're aren't hoisted.
Function expressions and declarations can be overwritten,
whereas classes can be extended but not overwritten.
For example, you can't override
the doors property in a Car class.
These are the major differences between the two
so here are a few best practices and use cases.
When you want to create a blueprint to be reused
over and over and still need functions,
create a class and add methods to it.
A good example are React Class Components,
where we use them to create and use these components
with life cycle methods available to us
in our React applications.
We build the component by extending the class component.
When you need to create a brand new function,
not requiring any specific properties or methods,
then a function is better.
Another example related to React is when we use
a stateless functional component, which is,
in fact, a pure function.
A great way to practice and start thinking
when to use one over the other
is to build an application with Reacts library,
where you'll always have to think about
when to use a stateless versus a stateful component,
which is the equivalent of a function and a class.