Skip to content

Instantly share code, notes, and snippets.

@dustinpoissant
Created September 13, 2016 14:02
Show Gist options
  • Save dustinpoissant/c04d0e64ae25db966d8bac5c9f5c00ea to your computer and use it in GitHub Desktop.
Save dustinpoissant/c04d0e64ae25db966d8bac5c9f5c00ea to your computer and use it in GitHub Desktop.
A JavaScript function that converts anything into a function.
function toFunc(v){
if(typeof(v) == "function") return v;
if(typeof(v) == "string"){
if(
window[v] != undefined &&
typeof(window[v]) == "function"
){
return window[v];
}
try {
return new Function(v);
}catch(e){}
}
return function(){return v;};
};
function toFunc(a){if("function"==typeof a)return a;if("string"==typeof a){if(void 0!=window[a]&&"function"==typeof window[a])return window[a];try{return new Function(a)}catch(a){}}return function(){return a}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment