defineClass
by andrewdavey
HTML
<div id="output"></div>
JavaScript
var defineClass = (function() {
function copyOwnProperties(source, target) {
var property;
for (property in source) {
if (source.hasOwnProperty(property)) {
target[property] = source[property];
}
}
}
function allOwnPropertiesAreFunctions(object) {
var property;
for (property in object) {
if (object.hasOwnProperty(property) && typeof object[property] !== "function") {
return false;
}
}
return true;
}
function createProxy(members) {
var Proxy, property, defineProxyFunction;
// A Proxy object has a target object.
Proxy = function(target) {
this.__target__ = target;
};
defineProxyFunction = function(property) {
var member = members[property];
Proxy.prototype[property] = function() {
// Call the member, in the context of the Proxy's target object.
return member.apply(this.__target__, arguments);
};
};
// All function calls made on the Proxy are delegated to the target object.
for (property in members) {
if (members.hasOwnProperty(property)) {
defineProxyFunction(property);
}
}
return Proxy;
}
function defineClass(options) {
var PublicClass, Class, constructor, publics, privates;
constructor = (typeof options.constructor === "function") && options.constructor;
publics = options["public"] || {};
privates = options["private"] || {};
if (!allOwnPropertiesAreFunctions(publics)) {
throw new Error("All public members of a class must be functions.");
}
PublicClass = createProxy(publics);
Class = function() {
// When creating an instance of the...