Improving a JS object "framework" I have this simple object setup in JavaScript - Full code on jsbin

I'll break it up below, but the question(s) -


Does it break? I mean, I'm sure we can find a interesting case where this would be broken, but how much work do we have to do before it breaks?
I started out thinking I needed to explicitly store a list of classes (variable _heap below; yes, calling it heap is rather overdone for what it is) to keep the inheritance tree and avoid memory leaks when an object is instantiated multiple times. I've got the feeling it's unnecessary but I haven't found how to not use it...
Any points on how this could be better really! Is this very contrived code?


I'm trying to allow common object creation technique like:

function FourLegged() {

   this.CRY = "ffffhhhh!";

  // method
   this.talk = function() {
         writeln( this.CRY);
      };
}


But also to have the class, constructor and inheritance in a one block, as OO languages usually do:

function Cat() {

   // inheritance
   this._parent = FourLegged;

   // constructor
   this.construct =
      function(n) {
         // private var defined in instance
         var name;
         name = n;
         // methods that use private var - Douglas Crockford nethod
         // avoid it, it wastes memory if there are multiple instances
         this.setName = function(n) { name = n; };
         this.getName = function() { return name; };
      };

   // methods will operate in instance context (ie "this" is the instance)
   this.talk =
      function() {
         writeln( this.getName() + " says " + this.CRY);
      };

}


So to do this, I wrap the creation of a new instance:

cat1 = new instance(Cat, "felix");


The wrapper processes the function so as to use the constructor and inheritance, but also maintains the _heap (the class list):

function instance() {
   var c = getConstructor(arguments[0]);
   var args = Array.prototype.slice.call(arguments, 1);
   return new c(args);
}

var _heap = {};

function getConstructor(cls) {
   var cons;
   // try to retrieve from _heap
   if (!(cons = _heap[cls])) {
      // if not already in heap
      var c = new cls();
      if (c._parent) {
         // set up class-superclass relation
         cls.prototype = new instance(c._parent);
         // class has changed, re-intantiate
         c = new cls();
      }
      if (!c.construct) {  // if there is no constructor
         c.construct = function() {}; // make one
      }

      cons =
         function(args) {
            return c.construct.apply(this, args);
         };
      cons.prototype = c;  // set up class-instance relation

      _heap[cls] = cons;
   }
   return cons;
}
My perspective on your questions:


It breaks when you want to change the behavior of all cats, since you do not store functions in the .prototype of the classes. Also, not using .prototype makes your code consume more memory.
There is indeed a standard way of doing this, however it kind of breaks your requirement : "But also to have the class, constructor and inheritance in a one block, as OO languages usually do." 
I think vanilla JS does better and is less contrived.


Most of the code I borrowed from this excellent text.

Your FourLegged class would be:

function FourLegged() {
   this.cry = "ffffhhhh!"; //default value
}
//Functions/methods should be part of prototype
FourLegged.prototype.talk = function talk(){
  writeln( this.cry);
};


If you absolutely insist on doing this in 1 block, you could do this : 

var FourLegged = (function() {

  function f() {
     this.cry = "ffffhhhh!"; //default value
  }

  f.prototype.talk = function talk(){
    writeln( this.cry);
  };

  return f;
}());


Then the cat would be this ( wrapped in a function, not required )

var Cat = (function() {

  function f(name){

     FourLegged.call(this);

     (function nameProperty( o , name) {
       // private var defined in instance
       var _name = name;
       // methods that use private var - Douglas Crockford nethod
       o.setName = function(name) { _name = name; };
       o.getName = function() { return _name; };
    })(this , name);
  }

  f.prototype = new FourLegged();
  f.prototype.constructor = f;

  f.prototype.talk = function talk() {
    writeln( this.getName() + " says " + this.cry);
  };
  f.prototype.setCry = function setCry( s ) {
    this.cry = s;
  };

  return f;
}());


Your tests will perform exactly the same, except you do not need _parent, _heap, instance() or getConstructor().

Update

A clarification on 'it does not use memory'.When I create objects this way in the console in Chrome : 

function FourLegged() 
{
   this.CRY = "ffffhhhh!";
   this.talk = function() { writeln( this.CRY); };
}


I find that these object take 88 bytes. ( Take a head snapshot with developer tools ).

When I create objects the following way in the console in Chrome:

function FourLegged() 
{
   this.CRY = "ffffhhhh!";
}
FourLegged.prototype.talk = function() { writeln( this.CRY); };


These objects take 48 bytes, and that is because the talk function remains on prototype, it does not get copied over the object.