[英]Can I simplify multiple if - else statements?
I have the following code in javascript? 我在javascript中有以下代码?
if ($(form).data('action') == "Edit") {
xx
} else if ($(form).data('action') == "Create"){
xy
} else if ($(form).data('action') == "Delete"){
xz
}
Is it possible for me to simplify this and also have some default path? 我可以简化这个并且还有一些默认路径吗?
You could also create an object that contains your actions: 您还可以创建包含操作的对象:
var Actions = {
'Edit' : function () {},
'Create' : function () {},
'Delete' : function () {}
};
var action = $(form).data('action');
if (Actions.hasOwnProperty(action)) {
Actions[action]();
}
What it sounds like you're describing is a switch/case, but I don't find switch case to be any better than multiple if/else structures. 你所描述的是一个开关/案例,但我发现switch case比多个if / else结构更好。 I prefer using object hashes:
我更喜欢使用对象哈希:
var actionObj = {
"Edit": xx,
"Create": xy,
"Delete": xz
};
if (actionObj[act]) {
// do whatever with actionObj[act] you need to
} else {
// do your default action
}
It works especially well when the values are actually functions, then you can just call them: 当值实际上是函数时,它工作得特别好,然后你可以调用它们:
var actionObj = {
"Edit": function () {},
"Create": function () {},
"Delete": function () {}
};
if (actionObj[act]) {
actionObj[act]();
} else {
// default action
}
Use Switch if you want to handle multiple if else 如果要处理多个if else,请使用Switch
switch(n)
{
case 1:
execute code block 1
break;
case 2:
execute code block 2
break;
default:
code to be executed if n is different from case 1 and 2
}
You should be caching the results of $(form).data('action'); 你应该缓存$(form).data('action')的结果; Example:
例:
var action = $(form).data('action');
if (action === 'Edit') {
//
} else if (action === 'Create') {
//
} else if (action === 'Delete') {
//
}
Using a switch
block is the most conventional way. 使用
switch
块是最常规的方式。
You can also lookup functions to execute from an object (hashmap): 您还可以查找要从对象执行的函数(hashmap):
var actions = {
Edit: function () {
// xx
},
Create: function () {
// xy
},
Delete: function () {
// xz
}
};
var action = actions[$(form).data("action")];
if (action) {
action();
} else {
// unknown/default action
}
use switch case : 使用开关盒:
var action = $(form).data('action')
switch(action)
{
case 'Edit': xx;
break;
case 'Create': yy;
break;
case 'Delete': zz;
break;
default: caption ="default";
}
or you can use ternary operator ( true ? 1 : 0 ) 或者您可以使用三元运算符(true?1:0)
var Actions = {
'Edit' : function () {},
'Create' : function () {},
'Delete' : function () {},
'default': function () {}
},
action = $(form).data('action');
Actions[action]() || Actions[default]();
Try CASE from here: http://www.tutorialspoint.com/javascript/javascript_switch_case.htm 从这里尝试CASE: http : //www.tutorialspoint.com/javascript/javascript_switch_case.htm
Example from them: 他们的例子:
<script type="text/javascript">
<!--
var grade='A';
document.write("Entering switch block<br />");
switch (grade)
{
case 'A': document.write("Good job<br />");
break;
case 'B': document.write("Pretty good<br />");
break;
case 'C': document.write("Passed<br />");
break;
case 'D': document.write("Not so good<br />");
break;
case 'F': document.write("Failed<br />");
break;
default: document.write("Unknown grade<br />")
}
document.write("Exiting switch block");
//-->
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.