Skip to content

Instantly share code, notes, and snippets.

@mattmccray
Created April 27, 2010 20:54
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 mattmccray/381313 to your computer and use it in GitHub Desktop.
Save mattmccray/381313 to your computer and use it in GitHub Desktop.
Tiny JS module/class framework.
/*
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)));
};
}
};
/*
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