Created
April 27, 2010 20:54
-
-
Save mattmccray/381313 to your computer and use it in GitHub Desktop.
Tiny JS module/class framework.
This file contains 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 characters
/* | |
Version 0.2 | |
module("Boldr", function(ns) { with(ns) { | |
module("Hello", function() { | |
def("hello", function() { | |
alert("hello " + this.name); | |
}); | |
}); | |
klass("MyClass", function() { | |
include(Boldr.Hello); | |
def("initialize", function(name) { | |
this.name = name; | |
}); | |
}); | |
}}); | |
var myClass = new Boldr.MyClass("world"); | |
myClass.hello(); | |
*/ | |
function module(name, definition) { | |
var current_constructor; | |
if (!window[name]) { | |
window[name] = {}; | |
}; | |
var module_stack = [window[name]]; | |
function current_module() { | |
return module_stack[module_stack.length -1]; | |
}; | |
function push_module(name) { | |
if (!current_module()[name]) { | |
current_module()[name] = {}; | |
}; | |
var module = current_module()[name]; | |
module_stack.push(module); | |
return module; | |
}; | |
function pop_module() { | |
module_stack.pop(); | |
}; | |
var keywords = { | |
module: function(name, definition) { | |
var module = push_module(name); | |
definition.call(module); | |
pop_module(); | |
}, | |
klass: function(name, definition) { | |
current_constructor = function() { | |
if (this.initialize) { | |
this.initialize.apply(this, arguments); | |
}; | |
}; | |
definition.call(current_constructor); | |
this.include(module.mixins); // ?? | |
current_module()[name] = current_constructor; | |
current_constructor = undefined; | |
}, | |
include: function(mixin) { | |
for (var slot_name in mixin) { | |
current_constructor.prototype[slot_name] = mixin[slot_name]; | |
}; | |
}, | |
def: function(name, fn) { | |
if(current_constructor) { | |
current_constructor.prototype[name] = fn; | |
} else { | |
current_module()[name] = fn; | |
}; | |
} | |
}; | |
definition.call(current_module(), keywords); | |
}; | |
module.mixins = { | |
method: function() { | |
var self = this, | |
args = Array.prototype.slice.call(arguments), | |
meth = self[args.shift()]; | |
return function curriedMethod() { | |
return meth.apply(self, args.concat(Array.prototype.slice.call(arguments))); | |
}; | |
} | |
}; |
This file contains 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 characters
/* | |
Version 0.2 | |
module("Boldr", function(ns) { with(ns) { | |
module("Hello", function() { | |
def("hello", function() { | |
alert("hello " + this.name); | |
}); | |
}); | |
klass("MyClass", function() { | |
include(Boldr.Hello); | |
def("initialize", function(name) { | |
this.name = name; | |
}); | |
}); | |
}}); | |
var myClass = new Boldr.MyClass("world"); | |
myClass.hello(); | |
*/ | |
function module(name,definition){var current_constructor;if(!window[name]){window[name]={};};var module_stack=[window[name]];function current_module(){return module_stack[module_stack.length-1];};function push_module(name){if(!current_module()[name]){current_module()[name]={};};var module=current_module()[name];module_stack.push(module);return module;};function pop_module(){module_stack.pop();};var keywords={module:function(name,definition){var module=push_module(name);definition.call(module);pop_module();},klass:function(name,definition){current_constructor=function(){if(this.initialize){this.initialize.apply(this,arguments);};};definition.call(current_constructor);this.include(module.mixins);current_module()[name]=current_constructor;current_constructor=undefined;},include:function(mixin){for(var slot_name in mixin){current_constructor.prototype[slot_name]=mixin[slot_name];};},def:function(name,fn){if(current_constructor){current_constructor.prototype[name]=fn;}else{current_module()[name]=fn;};}};definition.call(current_module(),keywords);};module.mixins={method:function(){var self=this,args=Array.prototype.slice.call(arguments),meth=self[args.shift()];return function curriedMethod(){return meth.apply(self,args.concat(Array.prototype.slice.call(arguments)));};}}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment