Intro to Object Oriented Programming

After this lesson you should…

  • Have a high-level understanding of what OOP is
  • Be able to speak to the benefits of OOP
  • Understand inheritance


  • OOP Object Oriented Programming
  • Object A grouping of data and behavior
  • Inheritance The practice of allowing new objects to take on the data and behavior of their parents
  • Encapsulation Hiding the details of how an object works & grouping data and behavior
  • Instance One specific copy of an object
  • Message Passing The way in which Objects talk to each other
  • SRP Single Responsibility Principle
  • Coupling The level of connectedness between two objects

What is Object Oriented Programming?

Here’s a definition from Techtarget:
  • Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic.
Here’s another definition from
  • OOP (not Oops!) refers to a programming methodology based on objects, instead of just functions and procedures. These objects are organized into classes, which allow individual objects to be group together.

While some languages such as Java, C++ and Ruby are specifically object-oriented languages, javascript is very flexible and has the ability to be functional (partial functions, currying, etc) or object oriented.


vehicle example

car example

instrument example


  • Code reusability
  • Encapsulation: values are scoped to the specific object
  • Design & Scalability: OOP forces programmers to meticulously plan the project. OOP is also much more maintainable for larger programs
  • Maintainable: OOP tends to be easier to modify specific pieces of the code without affecting the larger program

How do objects work?

object breakdown

When you’re considering creating an object you should look at the Law of Demeter or the Principal of Least Knowledge. If you’re wondering what that is you should probably check it out here. It can also be summarized by the following:

  • Each unit should have only limited knowledge about other units: only units “closely” related to the current unit.
  • Each unit should only talk to its friends; don’t talk to strangers.
  • Only talk to your immediate friends.

Objects and Instances

You want to think of an object like it were a template and an instance of that object is a specific version or type of that original template.

TRY IT: With someone next to you, brainstorm five types of objects and specific instances of that object that are at Turing. For example:

Type of object: Cubby

Specific instances:

  • alter-nate’s cubby
  • limbo’s cubby
  • student cubbies

Type of object: Refrigerator

Specific objects:

  • Staff refrigerator
  • Staff kegerator
  • Mod-specific refrigerators

In the cases above, what we called “type of object” is called a Class, and what we called “specific objects” are called instances.


is when an object or class is based on another object (prototypal inheritance) or class (class-based inheritance), using the same implementation (inheriting from an object or class: inheriting behavior, programming by difference[1]) or specifying a new implementation to maintain the same behavior (realizing an interface). Such an inherited class is called a subclass of its parent class or super class.

Or, just…

When an object or class is based on another object or class.

So back to the car examples above…Although each new instance of the car class has unique attributes, it may come with one or more shared attributes given from its parent. For example, each instance might come stock with:

  • 4 wheels
  • Steering wheel
  • Seat-belts


Old Way:

function Product(name, price) { = name;
  this.price = price;
  this.thing = 'stuff';

function Food(name, price) {, name, price);
  this.category = 'food';

New Way:

class Product {
  constructor(name, price) { = name;
    this.price = price;
    this.thing = 'stuff'

class Food extends Product {
  constructor(name, price) {
    super(name, price)


message passing

message example

This is how objects interact with each other. Now having one object just simply isn’t enough. In real life this process isn’t too difficult to understand. If I’m being annoying my wife communicates to me that I am being annoying and that I need to stop. If I’m driving my car and the gas tank light comes on it’s pretty apparent that I need to then put gas in the car.

In order for objects to interact with each other they must communicate with messages. Messages are parameters that are essentially passed back and forth from object to object. The messages use parameters to make sure the information is precise. If the receiving object does not have enough information, it will not be able to properly carry out the method

SRP and Coupling

Objects have expectations. When creating objects, you should always strive to have them know as little as possible or basically follow the SRP (Single Responsibility Principle). The more each object knows essentially the more dependencies that are added. That means that there is a higher likely hood that something will break.

Coupling refers to the level of connectedness between two objects. Objects will need to interact with one another and therefore can create dependencies. A good goal as a programmer is to make objects as independent as possible, meaning they can be tested as stand-alone units and don’t have too many dependencies on other objects to perform their respective duties.

Code Along

Let’s build some stuff!

Take a look at the Flash Cards Project

Set Up:

Make a new directory

$ mkdir oop-practice && cd oop-practice

Create a few files:

  • Card.js
  • Guess.js
  • Deck.js
  • Round.js
  • test.js
$ touch Card.js Guess.js Deck.js Round.js test.js

Create a package.json file

$ npm init

You can press enter to skip through all of the questions except for test command. For that enter mocha

Next, you’ll need to install chai in order to run our tests.

$ npm i --save-dev chai

Lastly, we’ll need to write tests in order to determine if our code works correctly. I strongly encourage you to write your own, but if you need, you can paste this into your test.js file to get going on creating objects.

const { expect } = require('chai')
const Card = require('./Card.js')
const Guess = require('./Guess.js')
const Deck = require('./Deck.js')
const Round = require('./Round.js')

describe('Card', () => {
  let card;

  beforeEach(() => {
    card = new Card ('What is the capital of Alaska?', 'Juneau')

  it('should exist', () => {

  it('should take a question and answer argument', () => {
    expect(card.question).to.equal('What is the capital of Alaska?')


describe('Guess', () => {
  let card;

  beforeEach(() => {
    card = new Card ('What is the capital of Alaska?', 'Juneau')

  it('should take in a response and card object as args', () => {
   const guess = new Guess('Juneau', card)


  it('should provide a positive reponse if guess is correct', () => {
    const guess = new Guess('Juneau', card)


  it('should provide feedback if guess is incorrect', () => {
    const guess = new Guess('Grizzley', card)



describe('Deck', () => {
  let card1,

  beforeEach(() => {
    card1 = new Card ('What is the capital of Alaska?', 'Juneau')
    card2 = new Card ('What is the capital of Minnesota?', 'St. Paul')
    card3 = new Card ('What is the capital of Colorado?', 'Denver')

  it('should add a new deck of cards as an array', () => {
    const cards = [card1, card2, card3]
    const deck = new Deck(cards)


  it('should have a count method', () => {
    const cards = [card1, card2, card3]
    const deck = new Deck(cards)


describe('Round', () => {
  let card1, card2, card3, cards, deck, round

  beforeEach(() => {
    card1 = new Card ('What is the capital of Alaska?', 'Juneau')
    card2 = new Card ('What is the capital of Minnesota?', 'St. Paul')
    card3 = new Card ('What is the capital of Colorado?', 'Denver')
    cards = [card1, card2, card3]
    deck = new Deck(cards)
    round = new Round(deck)

  it('should take in a new deck', () => {

  it('should show current card', () => {



  it('should log guesses', () => {
    const guess = 'Juneau'
    const expectedGuess = new Guess(guess, round.currentCard())




  it('should log correct guesses count', () => {
    const guess = 'Juneau'
    const correctThirdGuess = 'Denver'



    round.recordGuess('wrong guess')




  it('should provide number correct as percent', () => {
    const guess = 'Juneau'





Lesson Search Results

Showing top 10 results