Click here to view as a presentation
Students will be able to:
- Describe the use case for classes
- Describe encapsulation in OOP
- Define a class
- Instantiate a class
- Include and use a constructor method in a class
- Define prototype (instance) methods in a class
- Recognize constructor functions (predecessor to classes)
- Define static (class) methods
- Use extends to create a subclass
- Use super within a subclass
-
In object oriented programming (OOP), we use objects to model our application's purpose.
-
Classes (as well as their predecessor, constructor functions) are used to create objects.
-
Think of classes as the blueprints used to create objects of a certain "type"...
-
We've already been creating JS objects using object ___________ notation.
-
So why do we need classes and/or constructor functions?
-
Because the number of a certain type of object needed by an application often varies at runtime; and...
-
Classes/constructors provide a convenient way to dynamically create objects as needed.
-
Encapsulation is a key principle of Object Oriented Programming.
-
Encapsulation is the concept of bundling data (properties/attributes) and related behavior (methods) within an object.
-
Here comes a graphic depicting this principle...
- Here's a code example of encapsulating data (attributes/properties) & behavior (methods):
const cohort = {
id: 'SEI',
students: ['Mary', 'Toni', 'Fred'],
instructors: ['Susan', 'Phil'],
addStudent: function(name) {
name = name[0].toUpperCase() + name.substr(1).toLowerCase();
this.students.push(name);
},
pickRandomStudent: function() {
let rndIdx = Math.floor(Math.random() * this.students.length);
return this.students[rndIdx];
}
};-
What does the acronym OOP stand for?
-
In your own words, describe why Classes exist in OOP.
-
In your own words, describe the OOP principle known as encapsulation.
-
Here's a minimal class definition that's good for nothing but creating empty objects:
class Vehicle { // Code to define the class's properties and methods }
-
Looks similar to defining a function because classes are in fact, special functions, except...
What's different compared to a function?
What's different about the naming convention?
-
Here's a bit more OOP vocab for you:
-
instance: An object created by a class
-
instantiate: We instantiate a class to create an object
-
instantiation: The process of creating an object
-
-
In JS, we create objects using the
newkeyword when invoking (instantiating) the class:let v1 = new Vehicle();
Let's use repl.it to try out the minimal
Vehicleclass
-
When a class is being instantiated, the special
constructormethod in the class will automatically be called:class Vehicle { constructor(vin, make) { this.vin = vin; this.make = make; // return is not needed // because the new object is returned by default } } let plane = new Vehicle('X123Y', 'Boeing');
-
The purpose of the
constructormethod is to initialize the data properties of the new object being created (represented bythis). -
If there are no properties to initialize, the
constructormethod is optional (a hidden default constructor is called).
-
Modify the
Vehicleclass by adding an additional property namedmodel. -
Test it out by instantiating another object like this:
let car = new Vehicle('A1234', 'Toyota', 'Camry');
-
When we invoke the class prefaced with the
newkeyword, behind the scenes:- JS creates a shiny new object (empty) and assigns it to the
thiskeyword. - The
constructormethod is called with the arguments we provided when invoking the class. Remember, theconstructormethod is where we create/initialize properties on the new object assigned tothis. - After the
constructoris finished executing, the class automatically returns the shiny new object.
- JS creates a shiny new object (empty) and assigns it to the
-
Although the
constructormethod is special because it's called automatically, there's nothing special about how it's defined, other methods are defined the same way...
-
There are two types of methods that can be added to a class:
- Prototype (instance) methods, and
- Static (class) methods
-
Prototype methods are the most common and are available to be called by any instance of the class.
What's an instance? -
Static methods are methods that are called on the class itself and cannot be called by instances.
-
Let's add a
startmethod to ourVehicleclass:class Vehicle { // the constructor will always be called constructor(vin, make, model) { this.vin = vin; this.make = make; this.model = model; this.running = false; // default to false } start() { this.running = true; console.log('running...'); } }
-
Note that unlike within object literals, methods are not separated by a comma.
- Define a
stopmethod that sets therunningproperty tofalseand console.logs the message "stopped..."
-
Thanks to another OOP principle called inheritance, subclasses inherit methods from their parent classes.
-
JS is different from class-based languages like Java or Python in that its inheritance implementation is prototype-based. We won't go into prototypes during this lesson, but if you want to learn more, check out the docs here.
-
In JS, virtually every object inherits from the
Objectclass and thus inherits it's methods, such astoString:car.toString() // outputs something like '[object Object]'
-
If we define a method that already exists in the object hierarchy, we "override" it. For example, we can override the Object's
toStringmethod by adding it to our class:// existing methods above toString() { return 'Vehicle (' + this.vin + ') is a ' + this.make + ' model ' + this.model; }
Test it out.
- You've just learned how to define a class and add prototype methods to it. This represents about 80% there is to know about classes - congrats!
Some questions before moving on:
- What is the JS keyword used to define a class?
- What is the name of the method in a class that is automatically called when we instantiate a class?
- What is the main purpose of this method?
- What character(s) separate the methods in a class definition?
-
Before classes arrived via ES2015, we used constructor functions to do the exact same thing as classes.
-
Because of the newness of ES2015, much of the code out there is written using constructor functions, however, most new code today is likely to be written as classes.
-
It's important that you be able to recognize constructor functions, so let's look at how the
Vehicleclass can be written as a constructor function...
function Vehicle(vin, make, model) {
this.vin = vin;
this.make = make;
this.model = model;
this.running = false; // default to false
}
Vehicle.prototype.start = function() {
this.running = true;
console.log('running...');
};
// other 'prototype' (instance) methods defined like above
let car = new Vehicle('A1234', 'Toyota', 'Camry');-
Note that constructor functions are similar to the constructor methods in a class. Also note how instance methods are defined on the function's prototype object.
-
Invoking a class and a constructor function works identically.
-
Again, static methods are methods that are callable on the class itself - not on its instances.
-
Static methods are used typically to implement behavior that does not pertain to a particular instance. For example, we could design the
Vehicleclass so that it tracks every vehicle it creates. We could then write static methods that return how many vehicles have been created, search for vehicles by their make, etc.
-
Here's how to define a basic static method:
static about() { alert("I'm the Vehicle class!"); }
Yup, the only difference is the
statickeyword -
As discussed, you invoke static methods on the class:
// invoke static methods on the class Vehicle.about(); // this will not work car.about();
-
Is there anything a class can implement that can't be done using constructor functions?
-
When using constructor functions, how are instance methods defined?
-
What's wrong with the following code?
class Shape { constructor(x, y) { this.x = x; this.y = y; } static getPosition() { return [this.x, this.y]; } }
-
Earlier we spoke briefly about inheritance.
-
In OOP, inheritance is when a "specialized" subclass is derived from a parent superclass, and thus inherits it's properties and methods.
-
For example, a
Paymentclass could haveCreditCard&Cashsubclasses derived from it.
-
We use the
extendskeyword to define a subclass:class Plane extends Vehicle { constructor(vin, make, model, airline) { super(vin, make, model); this.airline = airline; } engageAutoPilot() { console.log('Look Mom, no hands!'); } }
-
In a derived class, the
superkeyword represents the parent superclass and must be called before thethiskeyword can be used in the constructor.
-
Now we can create instances of
Planelike this:let spyPlane = new Plane('secret', 'Lockheed', 'SR-71', 'USA');
-
Note how the additional arguments used to initialize subclasses are always provided after those intended for the superclass(es).
- In complex systems, it's not uncommon to have several layers of inheritance - often referred to as an object hierarchy.
-
Define another subclass of the
Vehicleclass namedAutomobilewith an additional property ofnumDoorsand ahonkmethod. -
Test it out by instantiating it like this:
let fastCar = new Automobile('TS123Z', 'Tesla', 'P100D', 4);
-
Hint: It's okay to copy and paste your own code (but make sure you understand what it does)
-
Unlike function declarations, class declarations are not hoisted - they must be declared before using them to create objects.
-
It's possible to subclass JS's built-in "classes"! For example:
class Stack extends Array { get top() { return this[this.length - 1]; } }




