问题描述
我正在学习AngularJS,试图制作一个简单的计算器,并且试图将 if / else 的内容压缩为声明使用Java逻辑运算符(&&
,||
,!
)
I'm learning AngularJS, trying to make a simple calculator, and I'm trying to condense this if/else if statement to 1-2 lines, using the Javascript logical Operators (&&
, ||
, !
)
给出这个例子,我怎么减少它呢?(如果您不了解 $scope
,请忽略它.它基本上是一个视图.因此,当有人单击9时,计算器将在答案屏幕上显示9.)
Given this example, how could I reduce it?(if you don't understand $scope
, ignore it. Its basically a view. So when someone clicks 9, the calculator will display 9 on the answer screen)
$scope.setOperand = function (operandEntered) {
if ($scope.leftOperand === null) {
$scope.leftOperand = operandEntered;
}
else if ($scope.operator === null) {
$scope.leftOperand = $scope.leftOperand + operandEntered;
}
else if ($scope.rightOperand === null) {
$scope.rightOperand = operandEntered;
}
else if ($scope.answer === null) {
$scope.rightOperand = $scope.rightOperand + operandEntered;
}
};
推荐答案
TLDR;
做出与下面相同的假设,这将起作用,并且当然非常简单:
TLDR;
Making all the same assumptions as below, this will work and of course is dead simple:
$scope.setOperand = function (operand) {
var prop = $scope['leftOperand'] === null ? 'leftOperand' : 'rightOperand';
$scope[prop] = +$scope[prop] + operand;
};
关键是这一部分:+$scope[prop]
将null
强制转换为0,因此最终将其添加到一侧或另一侧(如果它是null
或具有值)(这似乎是逻辑上的意思)做). Bergi指出,两个值的null
和null
均未处理,但我想说应该在其他地方进行计算.
The key is this part: +$scope[prop]
That casts null
to 0, so you end up adding it to one side or the other, if it's null
or has a value (which is what the logic seems to do). Bergi pointed out that null
and null
for both values isn't handled, but I'd say the calculation should be done elsewhere:
$scope.calc = function(){
return eval(
+$scope.leftOperand // Cast if null to 0
+ ($scope.operator || '+') // Default to add if null
+$scope.rightOperand // Cast if null to 0
);
};
假设您有一个左/右操作数(并且您没有尝试执行多个操作):
Assuming you have one left/right operand (and you're not trying to do multiple operations):
var $scope = {
operator: '-',
answer: null,
leftOperand: null,
rightOperand: 3,
};
我们可以从以下内容开始:
We can start with:
$scope.setOperand = function (operand) {
var prop = ['leftOperand','rightOperand'].reduce(function(t, k) {
return $scope[k] === null ? k : t;
});
$scope[prop] = +$scope[prop] + operand;
};
https://jsfiddle.net/w89dLrqw/
这是四行.我们可以删除一行,然后加上一些小提示:
Which is four lines. We can remove one line with a little hijinks:
$scope.setOperand = function (operand) {
[['leftOperand','rightOperand'].reduce(function(t, k) {
return $scope[k] === null ? k : t;
})].map(function(prop){$scope[prop] = +$scope[prop] + operand});
};
https://jsfiddle.net/b63x7aag/
或者,如果您愿意,
$scope.setOperand = function (operand) {
[['leftOperand','rightOperand'].reduce(function(t, k) {return $scope[k] === null ? k : t;})]
.map(function(prop){$scope[prop] = +$scope[prop] + operand});
};
还有另一个(@bergi的道具):
And another one (props to @bergi):
$scope.setOperand = function (operand) {
(function(prop){$scope[prop] = +$scope[prop] + operand})
(['leftOperand','rightOperand'].reduce(function(t, k){return !+$scope[k] ? k : t}));
};
https://jsfiddle.net/mh1bvhcj/1/
最后两个看上去很小,最后一个运行为颠倒".我看不到以这种方式编写它只占用很少的水平空间是多么有用.
That last two look minified, and that last one runs "upside-down". I can't see how this is useful to write it this way just so it takes up very little horizontal space.
我不理解的是else if ($scope.answer === null)
的用途,因为据我所知,拥有answer === null
似乎并不会影响操作数.因此,这可能行不通,这取决于即将发生的事情.
What I don't understand is what the else if ($scope.answer === null)
is in there for, since having answer === null
doesn't seem like it would affect the operands as far as I can tell. So this may or may not work, depending on what that's about.
这篇关于使用带有javascript的逻辑运算符压缩if/else if语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!