Q:

How to give a specific context to a JavaScript function without invoking it right away

I'd like to create an object with some hidden (using closures) variables, and methods that utilize those hidden variables.  I don't however want to recreate those methods for each instantiation.  I'd like to have them in memory once.  I'm using plain functions to instantiate objects. 
Here's some code:
function Mario () {
  var mario = {}; // create the obj
  mario.name = "Mario"; // create a property on it
  mario.hp = (function () { // set up a closure
    var currentHp = Mario.baseHp * 100; // create a "hidden" variable
    return function (value) { // return the hp function that utilizes the hidden currentHp variable
      if (!! value) {
        return currentHp = ((currentHp * Mario.baseHp) + value);
      }
      return currentHp * Mario.baseHp;
    };
  }());
  return mario; // send along the newly created object
}
Mario.baseHp = 1;

var mario = Mario();
log(mario);
log(mario.hp());
log(mario.hp(-20));
log(mario.hp());

The problem here is that every time I create another "mario" object, I've created the hp function again in memory.  Here's my attempt at a solution so far:
function Mario () {
  var mario = {}; // create the obj
  mario.name = "Mario"; // create a property on it
  mario.hp = (function () { // set up a closure
    var currentHp = Mario.baseHp * 100; // create a "hidden" variable
    return Mario.hp; // reference the hp function below..... but the context is wrong, it needs access to currentHp variable. 
  }());
  return mario; // send along the newly created object. 
}
Mario.baseHp = 1;
Mario.hp = function (value) { // Create the hp function once in memory
  if (!! value) {
    return currentHp = ((currentHp * Mario.baseHp) + value);
  }
  return currentHp * Mario.baseHp;
};
var mario = Mario();
log(mario);
log(mario.hp());
log(mario.hp(-20));
log(mario.hp());

But obviously the context of Mario.hp is wrong.  I'm thinking there might be a way to use call or apply that would resolve this.  Any help would rock! 

A:

The usual solution is to use prototype and new :
function Mario () {
  this.name = "Mario"; // create a property on it
  this.baseHP = 33;
}
Mario.prototype.hp = function () { 
    var currentHP = this.baseHp * 100;
    ...
};

var mario = new Mario();
log(mario);
log(mario.hp());
log(mario.hp(-20));
log(mario.hp());

This creates what is usually called a class Mario.  Using new Mario() create instances of this classe, all of them using the same function hp (this in the function refer to the instance). 
EDIT 
If you want to store the currentHP variable and the hp function to return it, simply do this :
function Mario () {
  this.name = "Mario"; // create a property on it
  this.baseHP = 33;
  this.currentHP = this.baseHp * 100;
}
Mario.prototype.hp = function () { 
    return this.currentHP;
};

var mario = new Mario();
log(mario);
log(mario.hp());

