Skip to content

Instantly share code, notes, and snippets.

@mariusGundersen
Created March 8, 2012 08:47
Show Gist options
  • Save mariusGundersen/1999727 to your computer and use it in GitHub Desktop.
Save mariusGundersen/1999727 to your computer and use it in GitHub Desktop.
Expose makes it simple to expose functions and properties of AMD modules
define("expose", [], function(){
function toType(obj){
return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
return function(){
var list = arguments;
var func = /^function\s+([^(]+)/i;
var i=0;
var obj;
if(toType(list[0]) == "object"){
obj = list[0];
i++;
}else{
obj = {};
}
for(; i<list.length; i++){
obj[func.exec(list[i].toString())[1]] = list[i];
}
return obj;
}
});
//Example usage of expose.js
define("module1", ["expose"], function(expose){
function method1(){
//do stuff
}
function method2(){
//do other stuff
}
//instead of:
return {method1: method1,
method2: method2};
//do
return expose(method1,
method2);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment