Objects - user defined objects
Objects okay

Understand what a variable is?
Understand what a function is too?
Okay good.

An object is essentially a collection of variables (and methods - more on this later) that are stored in one place.

Say we have a battle system, most of the time we'll need a character, or player object or something like that.

var Jim = new Object();
Jim.name = "Jimmy";
Jim.hp = 50;
Jim.maxhp = 100;
Jim.atk = 6;

var Blob = new Object();
Blob.name = "Blob";
Blob.hp = 25;
Blob.maxhp = 50;
Blob.atk = 7;

// let's fight Jim against Blob shall we?

function simple_battle()
{
  var turn = 0;
  while (Jim.hp > 0 && Blob.hp > 0)
  {
    // change the turn
    if (++turn > 1)
      turn = 0;

    // handle attacking, deal damage
    switch(turn)
    {
      case (0):
        Blob.hp -= Jim.atk;
      break;
      case (1):
        Jim.hp -= Blob.atk;
      break;
    }

    // draw stats
    GetSystemFont().drawText(16,32, Jim.name +  " " + (Jim.hp / Jim.maxhp));
    GetSystemFont().drawText(16,48, Blob.name + " " + (Blob.hp / Blob.maxhp));

    FlipScreen();
    Wait();
  }

  if (Jim.hp > 0)
    return true;
  return false;
}

function Wait()
{
  while (AreKeysLeft()) GetKey(); GetKey();
}

There you have your first simple text based battle system as such.
Using objects!

But why would I want to use an object?
Simply, to keep things that should be together, together.
Such as in my example there, all the player (or monster) stats, such as hp, maxhp and atk were in the same place.

Now, if we want to improve that above, we do:-

function PlayerObject(name, hp, maxhp, atk)
{
  this.name = name;
  this.hp = hp;
  this.maxhp = maxhp;
  this.atk = atk;
}

And:-

var Jim  = new PlayerObject("Jimmy", 50, 100, 6);
var Blob = new PlayerObject("Blob",  25,  50, 7);

So instead of using new Object, and then listing out all the properties we need.
We use an object called PlayerObject to help us be lazy.
In short, if we ever have more than 1 object that is essentially the same thing, we're not being lazy enough.

So now we want to change our simple_battle so that the so called attack code is better.

PlayerObject.prototype.take_hp = function(amount)
{
  this.hp -= amount;
  if (this.hp < 0) this.hp = 0;
  if (this.hp > this.maxhp) this.hp = this.maxhp;
}

PlayerObject.prototype.give_hp = function(amount)
{
  this.hp += amount;
  if (this.hp < 0) this.hp = 0;
  if (this.hp > this.maxhp) this.hp = this.maxhp;
}

Notice how the give_hp method and the take_hp method are almost the same?
Only one line is different?
Let's make it better still...

PlayerObject.prototype.take_hp = function(amount)
{
  this.hp -= amount;
  this.on_stats_change();
}

PlayerObject.prototype.give_hp = function(amount)
{
  this.hp += amount;
  this.on_stats_change();
}

PlayerObject.prototype.on_stats_change = function()
{
  if (this.hp < 0) this.hp = 0;
  if (this.hp > this.maxhp) this.hp = this.maxhp;
}

Okay, let's go back to our simple_battle function...
And we change it to:-

function simple_battle()
{
  var turn = 0;
  while (Jim.hp > 0 && Blob.hp > 0)
  {
    // change the turn
    if (++turn > 1)
      turn = 0;

    // handle attacking, deal damage
    switch(turn)
    {
      case (0):
        Blob.take_hp(Jim.atk);
      break;
      case (1):
        Jim.take_hp(Blob.atk);
      break;
    }

    // draw stats
    GetSystemFont().drawText(16,32, Jim.name +  " " + (Jim.hp / Jim.maxhp));
    GetSystemFont().drawText(16,48, Blob.name + " " + (Blob.hp / Blob.maxhp));

    FlipScreen();
    Wait();
  }

  if (Jim.hp > 0)
    return true;
  return false;
}


So we've covered objects, creating user defined objects and prototyping object methods...
I know I haven't done anything great, I've just done the basics.