ES2015 Classes

Abbreviations
*[JS]: JavaScript
*[OOP]: Object-Oriented Programming
*[POJO]: Plain Old JavaScript Object
Introduction
Object-Oriented Programming structures data into classes. Understanding what classes are and how to use them is foundational to working in Object-Oriented Programming.
In this subunit, we'll explore how ES2015 implements classes. First, we'll cover concepts you're already familiar with, like objects, which are instances of classes - then we'll teach you everything else you need to know to get up to speed using classes. After that, we'll show you how you can create your own custom classes. The end of the subunit will be a project where you implement a class to model a vehicle - don't worry, this subunit might stretch the conceptual muscles of your brain, but modeling a vehicle class is less intense than it sounds. No one will have to drive it, and it doesn't have to be street legal.
Goals
Review how objects work in JavaScript
Define classes in JavaScript
Use classes to create instances that share functionality
Describe constructor functions and use them to create instances
Describe inheritance
Define commonly used OOP (object-orientated programming) terms
JS Objects Review
"Plain Old JavaScript Object" (POJO):
Can add functions as keys:
Can get arrays of keys, values, or [key, val] arrays.
Details You Should Know
Properties that do not exist in the object register as undefined.
(This causes issues when you attempt to invoke ()
or .access
them)
All keys get "stringified":
What is
o1[i]
?
(This gets even more confusing when using things like nested arrays as keys)
Mixing Data and Functionality
Functions and Data
Imagine some useful functions:
demo/triangles.js
This gets a bit messy, though - all those functions to keep track of!
Using a POJO
demo/triangle-pojo.js
For now:
this references to "this object"
So, we can helpfully mix data & functionality!
This is tidy: related functionality lives together
Annoying when we want more than one triangle
Classes
Classes are a "blueprint" of functionality:
demo/triangle-oo.js
demo/triangle-oo.js
Defines the methods each instance of Triangle will have
Make a new triangle with
new Triangle()
Can still add/look at arbitrary keys ("properties")
this is "the actual triangle in question"
Class names should be UpperCamelCase
Reduces confusion between triangle (an actual, individual triangle) and Triangle (the class of triangles)
A triangle is still an object:
But JS knows it's an "instance of" the Triangle class:
Constructors
Consider how we made an instance of our Triangle class:
demo/triangle-constructor.js
The method with the special name constructor is called when you make a new instance.
What Can You Do in the Constructor`
Whatever you want!
Common things:
Validate data
Assign properties
(Note you don't return anything from constructor function)
Methods
Functions placed in a class are "methods" (formally: "instance methods").
They have access to properties of object with this.
They can take arguments/return data like any other function.
A method can call another method:
Note: to call a method, you need to call it on this
Without this, calling getArea throws a ReferenceError - it is not in scope!
Inheritance and Super
demo/triangle-duplicate.js
demo/triangle-duplicate.js
demo/triangle-extends.js
demo/triangle-extends.js
Multi-Level Inheritance
demo/triangle-extends.js
demo/triangle-extends.js
Often end up with "class hierarchy"
Glossary
- Property
piece of data on an instance (e.g.
myTriangle.a
)most languages call this idea an "instance attribute"
- Parent/Superclass
more general class you inherit from
Rectangle might be a parent of Square
- Object-Oriented Programming
using classes & instances to manage data & functionality together
often makes it easier to manage complex software requirements
- Method
function defined by a class, can call on instance
most accurate to call these "instance methods"
- Instance
an individual instance; an array is "instance" of Array
- Inherit
ability to call methods/get properties defined on ancestors
- Class
blueprint for making instances
- Child/Subclass
more specific class (a Square is a special knd of Rectangle)