Skip to content

Instantly share code, notes, and snippets.

@Raynos
Created December 20, 2011 19:56

Revisions

  1. Raynos revised this gist Jan 20, 2012. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions klass.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    // https://github.com/Raynos/pd
    var pd = typeof window === "undefined" ? require("pd") : window.pd;

    var klass = (function() {
    // meta data name
    var meta = pd.Name();
  2. Raynos revised this gist Jan 20, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion klass.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ var klass = (function() {
    }

    // create new prototype object by inheriting from Parent
    var proto = pd.make(parent.prototype, child(privates, parent.prototype));
    var proto = pd.extend(Object.create(parent.prototype), child(privates, parent.prototype));

    // fix constructor <-> prototype link
    proto.constructor.prototype = proto;
  3. Raynos revised this gist Dec 20, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion klass.js
    Original file line number Diff line number Diff line change
    @@ -21,7 +21,7 @@ var klass = (function() {
    }

    // create new prototype object by inheriting from Parent
    var proto = pd.make(Parent.prototype, child(privates, parent.prototype));
    var proto = pd.make(parent.prototype, child(privates, parent.prototype));

    // fix constructor <-> prototype link
    proto.constructor.prototype = proto;
  4. Raynos revised this gist Dec 20, 2011. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion klass.js
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,35 @@
    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.make(Parent.prototype, child(privates, parent.prototype));

    // fix constructor <-> prototype link
    proto.constructor.prototype = proto;

    // get the constructor function
    var constructor = proto.constructor;
    meta(constructor).privates = privates;
    // store the privates as meta data on this constructor
    meta(constructor).privates = privates;
    return constructor;
    }
    })();
  5. Raynos revised this gist Dec 20, 2011. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions klass.js
    Original file line number Diff line number Diff line change
    @@ -15,8 +15,7 @@ var klass = (function() {
    var privates = pd.Name();
    }

    var proto = Object.create(parent.prototype);
    pd.extend(proto, child(privates, parent.prototype));
    var proto = pd.make(Parent.prototype, child(privates, parent.prototype));

    proto.constructor.prototype = proto;

  6. Raynos created this gist Dec 20, 2011.
    27 changes: 27 additions & 0 deletions klass.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    var klass = (function() {
    var meta = pd.Name();

    return klass;

    function klass(parent, child) {
    if (!child) {
    child = parent;
    parent = Object;
    }

    if (parent !== Object) {
    var privates = meta(parent).privates;
    } else {
    var privates = pd.Name();
    }

    var proto = Object.create(parent.prototype);
    pd.extend(proto, child(privates, parent.prototype));

    proto.constructor.prototype = proto;

    var constructor = proto.constructor;
    meta(constructor).privates = privates;
    return constructor;
    }
    })();