- [Instructor] Once you understand how to build and use
prototypes in JavaScript code,
they're relatively straightforward.
However, traditional constructors and prototypes
don't result in code that's particularly organized.
It can take some studying to dig into existing code,
to map out its prototype chain and exactly what
is inherited from where.
Recent updates to JavaScript known as ES6 include
a new syntax for building out classes
with the class keyword.
Now, JavaScript is not a class-based language
like Java for example.
In a class-based language, you build out objects
using classes and each object receives
set properties and behaviors from the class constructor.
This may sound similar to prototypal
inheritance in JavaScript,
but it's a fundamentally different model.
The new class keyword lets us build out objects using
syntax that's similar to what you'd see
in a class-based language.
However, under the hood JavaScript classes
provide easier syntax for building out
constructor functions and prototypes.
So they're what's commonly referred to as syntactic sugar.
We're still building out constructors and prototypes.
We're just using an easier syntax to do it.
This new syntax uses the keyword class to specify the name
of the class and optionally the keyword extends
to identify another class to inherit from.
And this syntax makes the prototype chain
obvious at a glance, which is already a win
in terms of code organization.
Then within the class definition,
we define a constructor along with any methods that objects
based on the class should inherit.
Even though we don't make distinctions in class syntax
between what's copied to the new object and what's linked
to in the prototype,
it's all taken care of for us under the hood.
Like a number of other ES6 tools, classes make it easier
for developers to write code and to understand
existing code.
And like all modern JavaScript
classes can be transpiled into older,
more widely supported code.