Revisions
-
tobeytailor revised this gist
Jul 14, 2010 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = -
tobeytailor revised this gist
Jul 14, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.apply(caller, arguments); } } // used to defer setup of superclass and plugins -
tobeytailor revised this gist
Jul 14, 2010 . 2 changed files with 20 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { 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; } } This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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!'); } -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 4 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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); }; -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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[klassName] = function Klass() { // called as a constructor if (this != context) { // allow the init method to return a different class/object return this.init && this.init.apply(this, arguments); } -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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. function augment(destination, source) { for (var key in source) { destination[key] = source[key]; -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) return Klass; Subclass.prototype = Superclass.prototype; Klass.prototype = new Subclass; return Klass.addPlugins(deferred._plugins); }; -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -53,7 +53,7 @@ }; // valueOf is called to setup // inheritance from a superclass deferred.valueOf = function() { var Superclass = deferred._super; if (Superclass) { -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 0 additions and 11 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. */ (function(global) { -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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() { } function def(klassName, context) { // create class on given context (defaults to global object) -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -57,13 +57,13 @@ // add static helper method Klass.addPlugins = addPlugins; // called as function when not // inheriting from a superclass deferred = function(plugins) { return Klass.addPlugins(plugins); }; // valueOf is called to setup // inheritance from a superclasss deferred.valueOf = function() { var Superclass = deferred._super; -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 47 additions and 26 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { 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) { 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 class on given context (defaults to global object) var Klass = (context || global)[klassName] = function Klass() { // 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 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); }; // 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; } return Klass.addPlugins(deferred._plugins); }; return deferred; } // expose -
jdalton revised this gist
Jul 14, 2010 . 2 changed files with 1 addition and 12 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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. */ (function(global) { This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ // Example def ('Person') ({ 'init': function(name) { -
jdalton revised this gist
Jul 14, 2010 . 2 changed files with 20 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -70,25 +70,3 @@ // expose global.def = def; })(this); This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); -
jdalton revised this gist
Jul 14, 2010 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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) destination[key] = source[key]; } -
jdalton revised this gist
Jul 14, 2010 . 2 changed files with 94 additions and 42 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,42 +0,0 @@ This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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(); -
tobeytailor revised this gist
Jul 13, 2010 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,9 +1,10 @@ /* * 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") ({ -
tobeytailor created this gist
Jul 13, 2010 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; };