Created
March 8, 2012 08:47
-
-
Save mariusGundersen/1999727 to your computer and use it in GitHub Desktop.
Expose makes it simple to expose functions and properties of AMD modules
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
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; | |
} | |
}); |
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
//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