Source: class.js

[Frames]
/**
 * @class
 * @summary Provides a nicer syntax for defining classes,
 *          with support for OO-style inheritance.
 * @description As JavaScript is not a real object-oriented language,
 *              "class definitions" have to be written by manually
 *              adding each member function to the
 *              <code>prototype</code> member which causes cumbersome
 *              code. This utility function allows for a more readable
 *              class definition.<br/>
 *
 *              <strong>Note:</strong> Currently, the API3.Class is
 *              not compatible with the 
 *              [JsDoc documentation generator]{@link https://github.com/jsdoc3/jsdoc},
 *              so to document any class it has to be rewritten into
 *              the normal style.
 * @param data The definition of the class, usually inline.
 * @example
 * // Former definition of the API3.Entity class
 * API3.Entity = API3.Class({
 *    // Parent class
 *    _super: API3.Template,
 *
 *    // Constructor
 *    _init: function(sharedAI, entity) {
 *      // Call inherited constructor, important to keep this:
 *      this._super.call(this, sharedAI.GetTemplate(entity.template));
 *
 *      // Regular constructor operation:
 *      this._templateName = entity.template;
 *      ...
 *    },
 *
 *    // Member functions
 *    toString: function() {
 *        return "[Entity " + this.id() + " " + this.templateName() + "]";
 *    },
 *
 *    id: function() {
 *        return this._entity.id;
 *    },
 *
 *    ...
 * });
 */
API3.Class = function(data) {
	var ctor;
	if (data._init)
		ctor = data._init;
	else
		ctor = function() { };

	if (data._super)
	{
		ctor.prototype = { "__proto__": data._super.prototype };
	}

	for (var key in data)
	{
		ctor.prototype[key] = data[key];
	}

	return ctor;
};

/* Test inheritance:
var A = Class({foo:1, bar:10});
print((new A).foo+" "+(new A).bar+"\n");
var B = Class({foo:2, bar:20});
print((new A).foo+" "+(new A).bar+"\n");
print((new B).foo+" "+(new B).bar+"\n");
var C = Class({_super:A, foo:3});
print((new A).foo+" "+(new A).bar+"\n");
print((new B).foo+" "+(new B).bar+"\n");
print((new C).foo+" "+(new C).bar+"\n");
//*/

// -------------------------------------------------------------------
//  End of file
// -------------------------------------------------------------------