我需要找到最简洁的方法,根据两个参数返回三个值中的一个。我认为转换声明是最简洁的,但请提出替代方案。
我有两个论点,type
和action
如果是action == 'close'
,那么返回的结果应该始终是'Hide'
如果是action == open
,那么需要更多的检查
如果action == 'open' && type == 'flight'
返回show more flights
如果action == 'open' && type == 'protection'
返回show protections
在不涉及多个嵌套if
语句的情况下,最简洁的方法是什么?整个语句已经在由三元调用的另一个if
语句中,所以我不想添加更多。
这是我目前的解决方案,有什么办法可以让这个更轻吗?
createShowLabels: function(testEnabled,action,type){
if (testEnabled){
if(action === 'open'){
switch(type) {
case 'flight':
return 'Show flight times';
case 'protection':
return 'What\'s this?';
}
} else {
return 'Hide';
}
} else {
// Defaults
if (action === 'open'){
return 'Show more';
} else {
return 'Show less';
}
}
},
发布于 2016-08-25 03:13:21
首先,让我们明确一点:“简洁”不是"good“、”可读性“或”可维护性“的同义词。有时,简洁的代码是好的、可读的和可维护的;有时,它不是。
考虑到您在注释中保证只有值才是列出的值,有几种方法。
在我自己的代码中,我可能会使用类似于FDavidov's answer的睡眠,但格式不同:
if (action == 'close') {
result = 'Hide';
} else if (type == 'flight') {
result = 'show more flights';
} else {
result = 'show protections';
}
但你要求最简洁的方式。条件运算符很简洁,但不一定非常容易调试:
result = action == 'close' ? 'Hide' : type == 'flight' ? 'show more flights' : 'show protections';
示例:
function test(action, type) {
return action == 'close' ? 'Hide' : type == 'flight' ? 'show more flights' : 'show protections';
}
console.log(test('close', 'flight'));
console.log(test('close', 'protections'));
console.log(test('open', 'flight'));
console.log(test('open', 'protections'));
这些都不那么简洁,但更易于配置。
可以使用查找对象:
var lookup = {
'close|flight': 'Hide',
'close|protections': 'Hide',
'open|flight': 'show more flights',
'open|protections': 'show protections'
};
result = lookup[action + '|' + type];
示例:
var lookup = {
'close|flight': 'Hide',
'close|protections': 'Hide',
'open|flight': 'show more flights',
'open|protections': 'show protections'
};
function test(action, type) {
return lookup[action + '|' + type];
}
console.log(test('close', 'flight'));
console.log(test('close', 'protections'));
console.log(test('open', 'flight'));
console.log(test('open', 'protections'));
或者就像你说的,一个开关:
switch (action + '|' + type) {
case 'close|flight': result = 'Hide'; break;
case 'close|protections': result = 'Hide'; break;
case 'open|flight': result = 'show more flights'; break;
case 'open|protections': result = 'show protections'; break;
};
示例:
function test(action, type) {
var result;
switch (action + '|' + type) {
case 'close|flight': result = 'Hide'; break;
case 'close|protections': result = 'Hide'; break;
case 'open|flight': result = 'show more flights'; break;
case 'open|protections': result = 'show protections'; break;
};
return result;
}
console.log(test('close', 'flight'));
console.log(test('close', 'protections'));
console.log(test('open', 'flight'));
console.log(test('open', 'protections'));
发布于 2016-08-25 03:16:02
如果您正在寻找短手短语,这不是您要寻找的。这是一种非常简单和可读的方式,可以编写您想要的东西:
if (action == 'close') {
return 'Hide';
}
else if (action = 'open') {
if (type = 'flight' ) {return 'show more flights'}
else if (type = 'protection') {return 'show protections' }
}
https://stackoverflow.com/questions/39143635
复制