Skip to content

Instantly share code, notes, and snippets.

@thecotne
Created June 24, 2014 07:26
Show Gist options
  • Save thecotne/0c9c971f38175d7548f1 to your computer and use it in GitHub Desktop.
Save thecotne/0c9c971f38175d7548f1 to your computer and use it in GitHub Desktop.
javascript function with defoult params
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