Created
June 24, 2014 07:26
-
-
Save thecotne/0c9c971f38175d7548f1 to your computer and use it in GitHub Desktop.
javascript function with defoult params
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 f(func){ | |
var func_str = func.toString(); | |
var params = func_str.match(/\((.+)\)/)[1].split(','); | |
var params_str = ''; | |
for(var key in params){ | |
var parsed_param = params[key].match(/(.+)(\/\*)(.+)(\*\/)/); | |
params_str += "var "+parsed_param[1]+"= (typeof "+parsed_param[1]+" == 'undefined' )?"+parsed_param[3]+":"+parsed_param[1]+";\n"; | |
} | |
func_str = func_str.replace(/\{/, '{\n\t' + params_str); | |
eval("var fun = "+func_str); | |
return fun; | |
}; | |
var ragaca = f(function(name/*'cotne'*/,lastname/*'nazarashvili'*/){ | |
alert(name +' '+ lastname); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment