【发布时间】:2011-05-18 23:34:58
【问题描述】:
所以,这种情况经常发生在我身上,但这是我最新的一个:
var generic_error = function(title,msg){
if(!title){ title= 'Oops!'; }
if(!msg) { msg = 'Something must have gone wrong, but no worries we\'re working on it!'; }
$.fancybox(
{
content:'\
<div class="error_alert">\
<h2>'+title+'</h2>\
<p>'+msg+'\
</div>'
});
}
有没有一种更简洁的方法来检查所有参数,如上面的title 和msg,或者将它们设置为可选,或者在函数中定义默认值,例如 PHP 是如何做到的?有时我可以有 10 个选项,而 if(!var){var='defaults'} x 10 很恶心...
【问题讨论】:
标签: javascript jquery function variables