Created
September 13, 2016 14:02
-
-
Save dustinpoissant/c04d0e64ae25db966d8bac5c9f5c00ea to your computer and use it in GitHub Desktop.
A JavaScript function that converts anything into a function.
This file contains hidden or 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
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;}; | |
}; |
This file contains hidden or 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
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