使用多个参数创建检查以产生三个不同的结果
Creating a check using multiple arguments to produce three different results
我需要找到最简洁的方法来根据两个参数返回三个值中的一个。我认为switch语句是最简洁的,但请建议其他选项。
我有两个参数,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';
}
}
},
首先,让我们弄清楚:"简洁"不是"好的"、"可读的"或"可维护的"的同义词。有时候,简洁的代码是好的、可读的、可维护的;有时,它不是。
鉴于您在注释中保证仅是列出的值,有几种方法:
在我自己的代码中,我可能会使用类似于FDavidov的答案,但格式不同:
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'));
如果您正在寻找速记短语,这不是您要找的。这是一种非常简单且可读的方式来编写你想要的内容:
if (action == 'close') {
return 'Hide';
}
else if (action = 'open') {
if (type = 'flight' ) {return 'show more flights'}
else if (type = 'protection') {return 'show protections' }
}
相关文章:
- 为什么grunt contrib connect的中间件选项的第三个参数是未定义的
- 如何使用PHP和JS级联三个下拉列表
- 灯光问题(使用三个.js)
- 在三个.js中创建球体(例如地球)的磁力线
- 什么'这是从第三个函数上的async 1st函数获得结果的更好方法
- javascript第三个XMLHttpRequest被拒绝.但前两个是允许的
- 将循环中的两个文本框相乘,并在第三个文本框上显示结果
- 根据前两个下拉ID显示第三个下拉列表
- 在数组JS中查找三个重复项
- Meteor和S3:如何通过一次上传/编辑定义三个不同的图像
- RxJS:连接三个承诺,区分结果
- PHP, MYSQL + javascript?生成基于三个用户SELECT输入的结果(price)
- 在三个javascript结果的前两个中添加图像
- 使用多个参数创建检查以产生三个不同的结果
- 如何对两个文本字段的值求和,并在第三个文本字段中显示结果
- 如何用三个结果的三元运算输出值
- 使用jQuery给第三个文本框分配结果值
- 在两个输入框中计算值,并使用javascript在第三个输入框中获得结果
- XMLHttpRequest.打开第三个参数false不给php mysql在iPad上的查询结果
- 如何在javascript中添加数组中的前三个元素并删除前面的结果