如果我想简化以下内容:
if ($rootScope.map.visible == 0 && $rootScope.user == $rootScope.mapuser) {
var owner = "me";
} else if ($rootScope.map.visible == 1) ) {
var owner = "me";
} else {
var owner = "you";
}
这会有用吗?
if ( ($rootScope.map.visible == 0 && $rootScope.user == $rootScope.mapuser) || ($rootScope.map.visible == 1) ) {
var owner = "me";
} else {
var owner = "you";
}
答案 0 :(得分:2)
这些陈述看起来与我相等。你可以用卡诺图来证明它:
A = $rootScope.map.visible == 0
B = $rootScope.user == $rootScope.mapuser
C = $rootScope.map.visible == 1
D = A & B
E = D || C
A B C D E
=====================
F F F F F
F F T F T
F T F F F
F T T F T
T F F F F
T F T F T
T T F T T
T T T T T
如果E == False,那么所有者=“你的”
如果E == True则所有者=“我”
答案 1 :(得分:0)
您实际上可以使用ternary operator并在一行代码中完成相同的操作:
var owner = $rootScope.map.visible==1 ||
($rootScope.map.visible==0 && $rootScope.user==$rootScope.mapuser) ?
"me" : "you";
或者,如果$rootScope.map.visible
的唯一可能值为0
和1
(看起来很可能是变量的名称):
var owner = $rootScope.map.visible || $rootScope.user==$rootScope.mapuser?"me":"you";