Mathew Kleppin
JavaScript Anyone?
Object Oriented Programming
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();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.
Class
Object
Property
Method
Constructor
Inheritance
// 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();There are three vehicles: car, motorcycle and bike.
Warrior.js
https://github.com/olistic/warriorjs
By Mathew Kleppin
Real life in code form