所以,这种情况经常发生在我身上,但这是我的最新消息:
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是icky ......
答案 0 :(得分:4)
略短但相当于你现在正在做的是使用“||” AKA“或”AKA“默认运营商”。
title = title || 'Oops!';
msg = msg || 'Something must have gone wrong, but no worries we\'re working on it!';
答案 1 :(得分:1)
我怀疑你会发现任何比if(!title)title='DefaultTitle'
更短更简单的函数参数。
但是,我甚至会使用较长的表单来使其更明确:if (title===null) title='DefaultTitle'
。
这是一个相关的问题和答案,但我认为这只会使你的代码更复杂。 How can I access local scope dynamically in javascript?
答案 2 :(得分:1)
您可以使用三元符号作为recommended by this article,但格式更简单:
var n = null;
!title ? title = 'Oops' : n;
你还有arguments[]
数组,它包含参数并且可以在循环中使用,如下所示:
function test(one,two,three) {
i=0;
while(typeof(arguments[i]) != 'undefined') {
alert(arguments[i++]);
}
}
test(40,27,399);
答案 3 :(得分:1)
switch (arguments.length) {
case 0: title = 'Oops';
case 1: message = 'Something must have gone wrong...';
}
答案 4 :(得分:0)
这是另一种方法。 argsOK函数有点复杂,但调用它很容易。
//-----------------------------------------------------
/*
PURPOSE Ensures a function received all required arguments, no extra
arguments & (if so specified) no arguments are empty.
RETURNS True if the arguments validate, else false.
*/
function argsOk(
functionCallee , // Caller's callee object
allArgsRequired , // True = All arguments are required
emptyArgsAllowed // True = Empty arguments are allowed
){
var ok = true;
for (var i = 0; i < 1; ++i) {
var functionName = functionCallee.toString().split(' ')[1].split('(')[0];
var args = functionCallee.arguments;
var expectedArgCount = functionCallee.length;
var actualArgCount = args.length;
if ((allArgsRequired && actualArgCount < expectedArgCount) ||
(actualArgCount > expectedArgCount)) {
error("Function " + functionName + " expected " + expectedArgCount + " arguments, but received " + actualArgCount + ".");
ok = false;
break;
}
if (emptyArgsAllowed) {
break;
}
for (var j = 0; j < args.length; ++j) {
if (args[j] != null && args[j].length == 0) {
error("Function " + functionName + "() received an empty argument.");
ok = false;
break;
}
}
}
return ok;
}
调用它的示例(单行,如您所见):
//------------------------------------------------
function image(item, name, better)
// PURPOSE Write a request for picture or photo
// ENTRY Init() has been called
{
if (!showingShortVersion()) {
var betterString = '';
if (better != null && better == true)
betterString = 'better ';
if (argsOk(arguments.callee, true, false))
write('<p class="request maintain">If you have ac­cess to a ' + betterString + item + ' of ' + name + ' that we could put on­line, please <a href="misc/pics.htm" onmouseover="return stat(\'Learn how to send us images\')" onmouseout="return erase()">click here</a>.</p>');
}
}
答案 5 :(得分:0)
一般来说,特别是在Javascript中,干净!=短。
请勿将if(!title){ title= 'Oops!'; }
用作常规解决方案,因为例如0
和空字符串也是假的。未设置的参数为undefined
,因此我更喜欢使用
if (title === undefined) {
title= 'Oops!';
}
这样做可能更加冗长,但它可以防止不必要的副作用,根据我的经验,如果您尝试使用快捷方式,Javascript会产生许多不必要的副作用。