Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from tbtlr/def.js
Created March 22, 2011 01:37

Revisions

  1. tobeytailor revised this gist Jul 14, 2010. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -45,6 +45,8 @@

    function def(klassName, context) {
    context || (context = global);

    if(context[klassName]) return context[klassName];

    // create class on given context (defaults to global object)
    var Klass =
  2. tobeytailor revised this gist Jul 14, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion def.js
    Original file line number Diff line number Diff line change
    @@ -8,7 +8,7 @@

    __super__ = function(){
    var caller = __super__.caller;
    if(caller !== caller._super){ return caller._super(); }
    if(caller !== caller._super){ return caller._super.apply(caller, arguments); }
    }

    // used to defer setup of superclass and plugins
  3. tobeytailor revised this gist Jul 14, 2010. 2 changed files with 20 additions and 1 deletion.
    18 changes: 17 additions & 1 deletion def.js
    Original file line number Diff line number Diff line change
    @@ -6,6 +6,11 @@
    */
    (function(global) {

    __super__ = function(){
    var caller = __super__.caller;
    if(caller !== caller._super){ return caller._super(); }
    }

    // used to defer setup of superclass and plugins
    var deferred;

    @@ -20,7 +25,18 @@
    // the one further up the prototype chain.
    function augment(destination, source) {
    for (var key in source) {
    destination[key] = source[key];
    var existing = destination[key], newValue = source[key];
    // check if we're overwriting an existing function
    destination[key] = typeof newValue == "function" && typeof existing == "function" ?
    (function(_super, base){
    return function(){
    var self = this;
    base._super = function(){
    _super.apply(self, arguments);
    }
    return base.apply(this, arguments);
    }
    })(existing, newValue) : newValue;
    }
    }

    3 changes: 3 additions & 0 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -10,6 +10,9 @@ def ('Person') ({
    });

    def ('Ninja') << Person ({
    'init': function(name) {
    __super__();
    },
    'kick': function() {
    this.speak('I kick u!');
    }
  4. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -58,8 +58,12 @@
    deferred.valueOf = function() {
    var Superclass = deferred._super;
    if (!Superclass) return Klass;

    Subclass.prototype = Superclass.prototype;
    Klass.prototype = new Subclass;

    Klass.superclass = Superclass;
    Klass.prototype.constructor = Klass;
    return Klass.addPlugins(deferred._plugins);
    };

  5. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -28,12 +28,13 @@
    function Subclass() { }

    function def(klassName, context) {
    context || (context = global);

    // create class on given context (defaults to global object)
    var Klass =
    (context || global)[klassName] = function Klass() {
    context[klassName] = function Klass() {
    // called as a constructor
    if (this != global) {
    if (this != context) {
    // allow the init method to return a different class/object
    return this.init && this.init.apply(this, arguments);
    }
  6. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,9 @@
    }

    // TODO: Ensure we fix IE to iterate over shadowed properties
    // of those further down the prototype chain. There is also a
    // of those further up the prototype chain. There is also a
    // bug in Safari 2 that will match the shadowed property and
    // the one further down the prototype chain.
    // the one further up the prototype chain.
    function augment(destination, source) {
    for (var key in source) {
    destination[key] = source[key];
  7. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 3 additions and 4 deletions.
    7 changes: 3 additions & 4 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -56,10 +56,9 @@
    // inheritance from a superclass
    deferred.valueOf = function() {
    var Superclass = deferred._super;
    if (Superclass) {
    Subclass.prototype = Superclass.prototype;
    Klass.prototype = new Subclass;
    }
    if (!Superclass) return Klass;
    Subclass.prototype = Superclass.prototype;
    Klass.prototype = new Subclass;
    return Klass.addPlugins(deferred._plugins);
    };

  8. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion def.js
    Original file line number Diff line number Diff line change
    @@ -53,7 +53,7 @@
    };

    // valueOf is called to setup
    // inheritance from a superclasss
    // inheritance from a superclass
    deferred.valueOf = function() {
    var Superclass = deferred._super;
    if (Superclass) {
  9. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 0 additions and 11 deletions.
    11 changes: 0 additions & 11 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,6 @@
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    *
    * Example:
    *
    * def ("Person") ({
    * init: function(){ ... }
    * });
    *
    * def ("Ninja") << Person ({
    * init: function(){ ... }
    * });
    */
    (function(global) {

  10. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,9 @@
    */
    (function(global) {

    // used to defer setup of superclass and plugins
    var deferred;

    function addPlugins(plugins) {
    augment(this.prototype, plugins);
    return this;
    @@ -35,9 +38,6 @@
    // dummy subclass
    function Subclass() { }

    // used to defer setup of superclass and plugins
    var deferred;

    function def(klassName, context) {

    // create class on given context (defaults to global object)
  11. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -57,13 +57,13 @@
    // add static helper method
    Klass.addPlugins = addPlugins;

    // use as function when not
    // called as function when not
    // inheriting from a superclass
    deferred = function(plugins) {
    return Klass.addPlugins(plugins);
    };

    // use valueOf to setup
    // valueOf is called to setup
    // inheritance from a superclasss
    deferred.valueOf = function() {
    var Superclass = deferred._super;
  12. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 47 additions and 26 deletions.
    73 changes: 47 additions & 26 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -3,57 +3,78 @@
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    *
    * Example:
    *
    * def ("Person") ({
    * init: function(){ ... }
    * });
    *
    * def ("Ninja") << Person ({
    * init: function(){ ... }
    * });
    */
    (function(global) {

    // dummy subclass
    function Subclass() { }
    function addPlugins(plugins) {
    augment(this.prototype, plugins);
    return this;
    }

    // TODO: Ensure we fix IE to iterate over shadowed properties
    // of those further down the prototype chain. There is also a
    // bug in Safari 2 that will match the shadowed property and
    // the one further down the prototype chain.
    function augment(destination, source) {
    // TODO: Ensure we fix IE to iterate over shadowed properties
    // of those further up the prototype chain. There is also a
    // bug in Safari 2 that will match the shadowed property and
    // the one further up the prototype chain.
    for (var key in source)
    for (var key in source) {
    destination[key] = source[key];
    }
    }

    // dummy subclass
    function Subclass() { }

    // used to defer setup of superclass and plugins
    var deferred;

    function def(klassName, context) {

    // create the `Class` on the given context (defaults to the global object)
    // create class on given context (defaults to global object)
    var Klass =
    (context || global)[klassName] = function Klass() {
    // called with the `new` operator
    // called as a constructor
    if (this != global) {
    // allow the init method to return a different class/object
    return this.init && this.init.apply(this, arguments);
    }

    // called as a method
    // defer setting up a superclass and adding plugins
    def._super = Klass;
    def._plugins = arguments[0] || { };
    },

    // static helper method
    addPlugins =
    Klass.addPlugins = function addPlugins(plugins) {
    augment(Klass.prototype, plugins);
    // defer setup of superclass and plugins
    deferred._super = Klass;
    deferred._plugins = arguments[0] || { };
    };

    // add static helper method
    Klass.addPlugins = addPlugins;

    // use as function when not
    // inheriting from a superclass
    deferred = function(plugins) {
    return Klass.addPlugins(plugins);
    };

    // we use valueOf to finish the setup of the
    // deferred superclass and added plugins
    addPlugins.valueOf = function() {
    var Superclass = def._super;
    // use valueOf to setup
    // inheritance from a superclasss
    deferred.valueOf = function() {
    var Superclass = deferred._super;
    if (Superclass) {
    Subclass.prototype = Superclass.prototype;
    Klass.prototype = new Subclass;
    }
    addPlugins(def._plugins);
    def._super = def._plugins = null;
    return Klass.addPlugins(deferred._plugins);
    };

    return addPlugins;
    return deferred;
    }

    // expose
  13. @jdalton jdalton revised this gist Jul 14, 2010. 2 changed files with 1 addition and 12 deletions.
    11 changes: 0 additions & 11 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -3,17 +3,6 @@
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    *
    * Example:
    *
    * def ("Person") ({
    * init: function(){ ... }
    * });
    *
    * def ("Ninja") << Person ({
    * init: function(){ ... }
    * });
    */
    (function(global) {

    2 changes: 1 addition & 1 deletion gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    // Usage example
    // Example

    def ('Person') ({
    'init': function(name) {
  14. @jdalton jdalton revised this gist Jul 14, 2010. 2 changed files with 20 additions and 22 deletions.
    22 changes: 0 additions & 22 deletions gistfile1.js → def.js
    Original file line number Diff line number Diff line change
    @@ -70,25 +70,3 @@
    // expose
    global.def = def;
    })(this);


    // Usage

    def ('Person') ({
    'init': function(name) {
    this.name = name;
    },
    'speak': function(text) {
    alert(text || 'Hi, my name is ' + this.name);
    }
    });

    def ('Ninja') << Person ({
    'kick': function() {
    this.speak('I kick u!');
    }
    });

    var ninjy = new Ninja('JDD');
    ninjy.speak();
    ninjy.kick();
    20 changes: 20 additions & 0 deletions gistfile2.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    // Usage example

    def ('Person') ({
    'init': function(name) {
    this.name = name;
    },
    'speak': function(text) {
    alert(text || 'Hi, my name is ' + this.name);
    }
    });

    def ('Ninja') << Person ({
    'kick': function() {
    this.speak('I kick u!');
    }
    });

    var ninjy = new Ninja('JDD');
    ninjy.speak();
    ninjy.kick();
  15. @jdalton jdalton revised this gist Jul 14, 2010. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -22,9 +22,9 @@

    function augment(destination, source) {
    // TODO: Ensure we fix IE to iterate over shadowed properties
    // of those further down the prototype chain. There is also a
    // of those further up the prototype chain. There is also a
    // bug in Safari 2 that will match the shadowed property and
    // the one further down the prototype chain.
    // the one further up the prototype chain.
    for (var key in source)
    destination[key] = source[key];
    }
  16. @jdalton jdalton revised this gist Jul 14, 2010. 2 changed files with 94 additions and 42 deletions.
    42 changes: 0 additions & 42 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -1,42 +0,0 @@
    /*
    * def.js: Simple Ruby-style inheritance for JavaScript
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    *
    * Example:
    *
    * def ("Person") ({
    * init: function(){ ... }
    * });
    *
    * def ("Ninja") << Person ({
    * init: function(){ ... }
    * });
    */

    def = function(className){
    var base = window[className] = function(){
    if(this.constructor === base){ if(this.init){ this.init(arguments); } }
    else{
    def._super = base;
    def._props = arguments[0];
    }
    },
    props = function(keyValues){
    if(def._super){
    var superclass = def._super;
    def._super = def._props = null;
    props(superclass.prototype);
    props(keyValues);
    }else{
    var proto = base.prototype;
    for(var key in keyValues){ proto[key] = keyValues[key]; }
    }
    };
    props.valueOf = function(){
    this(def._props);
    };
    return props;
    };
    94 changes: 94 additions & 0 deletions gistfile1.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,94 @@
    /*
    * def.js: Simple Ruby-style inheritance for JavaScript
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    *
    * Example:
    *
    * def ("Person") ({
    * init: function(){ ... }
    * });
    *
    * def ("Ninja") << Person ({
    * init: function(){ ... }
    * });
    */
    (function(global) {

    // dummy subclass
    function Subclass() { }

    function augment(destination, source) {
    // TODO: Ensure we fix IE to iterate over shadowed properties
    // of those further down the prototype chain. There is also a
    // bug in Safari 2 that will match the shadowed property and
    // the one further down the prototype chain.
    for (var key in source)
    destination[key] = source[key];
    }

    function def(klassName, context) {

    // create the `Class` on the given context (defaults to the global object)
    var Klass =
    (context || global)[klassName] = function Klass() {
    // called with the `new` operator
    if (this != global) {
    // allow the init method to return a different class/object
    return this.init && this.init.apply(this, arguments);
    }

    // called as a method
    // defer setting up a superclass and adding plugins
    def._super = Klass;
    def._plugins = arguments[0] || { };
    },

    // static helper method
    addPlugins =
    Klass.addPlugins = function addPlugins(plugins) {
    augment(Klass.prototype, plugins);
    };

    // we use valueOf to finish the setup of the
    // deferred superclass and added plugins
    addPlugins.valueOf = function() {
    var Superclass = def._super;
    if (Superclass) {
    Subclass.prototype = Superclass.prototype;
    Klass.prototype = new Subclass;
    }
    addPlugins(def._plugins);
    def._super = def._plugins = null;
    };

    return addPlugins;
    }

    // expose
    global.def = def;
    })(this);


    // Usage

    def ('Person') ({
    'init': function(name) {
    this.name = name;
    },
    'speak': function(text) {
    alert(text || 'Hi, my name is ' + this.name);
    }
    });

    def ('Ninja') << Person ({
    'kick': function() {
    this.speak('I kick u!');
    }
    });

    var ninjy = new Ninja('JDD');
    ninjy.speak();
    ninjy.kick();
  17. tobeytailor revised this gist Jul 13, 2010. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion def.js
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,10 @@
    /*
    * def.js: Simple Ruby style inheritance for JavaScript
    * def.js: Simple Ruby-style inheritance for JavaScript
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    *
    * Example:
    *
    * def ("Person") ({
  18. tobeytailor created this gist Jul 13, 2010.
    41 changes: 41 additions & 0 deletions def.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    /*
    * def.js: Simple Ruby style inheritance for JavaScript
    *
    * Copyright (c) 2010 Tobias Schneider
    * This script is freely distributable under the terms of the MIT license.
    *
    * Example:
    *
    * def ("Person") ({
    * init: function(){ ... }
    * });
    *
    * def ("Ninja") << Person ({
    * init: function(){ ... }
    * });
    */

    def = function(className){
    var base = window[className] = function(){
    if(this.constructor === base){ if(this.init){ this.init(arguments); } }
    else{
    def._super = base;
    def._props = arguments[0];
    }
    },
    props = function(keyValues){
    if(def._super){
    var superclass = def._super;
    def._super = def._props = null;
    props(superclass.prototype);
    props(keyValues);
    }else{
    var proto = base.prototype;
    for(var key in keyValues){ proto[key] = keyValues[key]; }
    }
    };
    props.valueOf = function(){
    this(def._props);
    };
    return props;
    };