Lesson 8

Object Oriented Programming

Thinking of Data Sets

One of the biggest questions a programmer must answer is:

"How are I going to model my data?"

Enter Object Oriented Programming.

 

In OOP, almost all interaction and data is stored as part of a larger object.

class Dragon {
  constructor: function(name, strength, size) {
    this.name = name;
    this.strength = strength;
    this.size = size;
  }

  fireBreath: function() {
    return 20*this.strength;
  }
}

var enemy = new Dragon('Valrogg', 10, 100);

var hp = 1000 - enemy.fireBreath();

An Introduction to OOP

This allows us to better picture how the different parts of our data will interact within the system we build.

 

OOP envisions software as a collection of cooperating objects rather than a collection of functions or simply a list of commands (what we've been doing so far).

 

In OOP, each object can receive messages, process data, and send messages to other objects. Each object can be viewed as an independent little machine with a distinct role or responsibility.

 

OOP promotes greater flexibility and maintainability in programming, and is widely popular in large-scale software engineering. Because OOP strongly emphasizes modularity, object-oriented code is simpler to develop and easier to understand later on. 

An Introduction to OOP

Class

  • Defines the object's characteristics. A class is a template definition of an object's properties and methods.

Object

  • An instance of a class.

Property

  • An object characteristic, such as color.

Method

  • An object capability, such as walk. It is a subroutine or function associated with a class.

Constructor

  • A method called at the moment an object is instantiated. It usually has the same name as the class containing it.

Inheritance

  • A class can inherit characteristics from another class.
// Class
class Dragon {

  /* constructor is a default method that is called when 
  creating a new Dragon object */
  constructor: function(name, strength, size) {

    //'this' refers to the object itself.
    this.name     = name;
    this.strength = strength;
    this.size     = size;
  }

  //the fireBreath method of our object.
  fireBreath: function() {
    return 20*this.strength;
  }
}

//Declaring the new object as a variable named enemy
var enemy = new Dragon('Valrogg', 10, 100);

var hp = 1000 - enemy.fireBreath();
/*extends is a type of inheritence. We are reusing the 
code from the dragon class here for our charmander */

class Charmander extends Dragon {
  constructor: function(level) {

    /* We want to talk to the dragon's constructor to have it 
    do its thing inside our charmander object*/

    super('Charmander', 10*level, 50);

    this.level = level;
  }

  scratch: function() {
    return 5 + strength;
  }
}

var enemyTwo = new Charmander(2);

//We maintain access to all of the dragon methods
hp = hp - enemyTwo.fireBreath();

Building Data

There are three vehicles: car, motorcycle and bike.

  • car and motorcycle have an engine, bike doesn't have an engine.
  • all vehicles can travel.
  • vehicles with an engine need to start their engine before traveling, and stop it after traveling.

Warrior.js

https://github.com/olistic/warriorjs

Lesson 8 - Objects & OOP

By Mathew Kleppin

Lesson 8 - Objects & OOP

Real life in code form

  • 485