Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created December 20, 2011 19:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Raynos/1502989 to your computer and use it in GitHub Desktop.
Save Raynos/1502989 to your computer and use it in GitHub Desktop.
klass
// https://github.com/Raynos/pd
var pd = typeof window === "undefined" ? require("pd") : window.pd;
var klass = (function() {
// meta data name
var meta = pd.Name();
return klass;
function klass(parent, child) {
// if one argument set parent to Object
if (!child) {
child = parent;
parent = Object;
}
if (parent !== Object) {
// extract privates from meta data on parent
var privates = meta(parent).privates;
} else {
// if parent is object create new name
var privates = pd.Name();
}
// create new prototype object by inheriting from Parent
var proto = pd.extend(Object.create(parent.prototype), child(privates, parent.prototype));
// fix constructor <-> prototype link
proto.constructor.prototype = proto;
// get the constructor function
var constructor = proto.constructor;
// store the privates as meta data on this constructor
meta(constructor).privates = privates;
return constructor;
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment