javascript 是否有使用 IF / ELSE 语句的替代方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19271755/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an alternative to using IF / ELSE statements
提问by mikedidthis
Question
问题
More out of curiosity, but I was wondering how to refactor an if statement to something cleaner / less brittle. From what I have read, polymorphismcould have a use?
更多的是出于好奇,但我想知道如何将 if 语句重构为更干净/更不脆弱的东西。从我读到的内容来看,多态可能有用吗?
In the example I only want to return the first car if color:'red'
is true.
在示例中,如果color:'red'
为真,我只想返回第一辆车。
Coffeescript
咖啡脚本
example: () ->
cars = [{color:'red', reg:'111'},{color:'blue', reg:'666'}]
if cars[0].color is 'red'
then cars[0]
else cars[1]
Javascript
Javascript
example: function() {
var cars = [{color:'red',reg:'111'},{color:'blue',reg:'666'}];
if (cars[0].color === 'red') {
return cars[0];
} else {
return cars[1];
}
}
I understand this question maybe closed or moved due to the ambiguous nature
我理解这个问题可能由于含糊不清而被关闭或移动
回答by Satpal
You can use ternary operator, its syntax is condition ? result1 : result2;
您可以使用三元运算符,其语法是 condition ? result1 : result2;
return cars[0].color === 'red' ? colors[0] : colors[1]
回答by Mike H.
? : operator is exactly that, a "cleaner" if-else
? : 运算符就是这样,一个“更干净”的 if-else
http://msdn.microsoft.com/en-us/library/ty67wk28.aspx
http://msdn.microsoft.com/en-us/library/ty67wk28.aspx
classify = (input < 0) ? "negative" : "positive";
There are also switch statements for larger combinations:
对于更大的组合,还有 switch 语句:
http://www.w3schools.com/js/js_switch.asp
http://www.w3schools.com/js/js_switch.asp
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
}
Polymorphism is an abstract concept, not a way to write a statement. It's the practice of creating a method/function/class/etc where type is at least SOMEWHAT ambiguous. So the same method could return a result if fed, say, an integer for parameter 1, the same as if you were to feed an array into the same parameter.
多态是一个抽象的概念,而不是一种写语句的方式。这是创建方法/函数/类/等的做法,其中类型至少有些不明确。因此,如果输入参数 1 的整数,则相同的方法可以返回结果,就像将数组输入相同的参数一样。
回答by leaf
Just for fun :
纯娱乐 :
// red -> +false -> 0
// not red -> +true -> 1
return cars[+(cars[0].color !== 'red')];
回答by Willem D'Haeseleer
Turning Car into an object:
把汽车变成一个物体:
function Car(options) {
this.options = {};
// Some default options for your object
$.extend(this.options, {
color: "green",
buildYear: 1990,
tires: 4,
brand: "merceded"
}, options);
}
// A method registered on the prototype
Car.prototype.getColor = function () {
return this.options.color;
};
var myToyota = new Car({
brand: "toyota"
});
console.log("My Toyota is: "+ myToyota.getColor());
example: http://jsfiddle.net/YthH8/
示例:http: //jsfiddle.net/YthH8/
Keep in mind that are are many ways you can use objects / inheritance in JavaScript.
Coffee script has it's own syntactic sugar for using classes => http://coffeescript.org/#classes
请记住,您可以通过多种方式在 JavaScript 中使用对象/继承。
咖啡脚本有它自己的语法糖来使用类 => http://coffeescript.org/#classes
回答by Snake Eyes
There is a ternar operator ?
used mostly when you don't want to use if-else
statement:
?
当您不想使用if-else
语句时,主要使用三元运算符:
example: function() {
var cars = [{color:'red',reg:'111'},{color:'blue',reg:'666'}];
return cars[0].color === 'red' ? cars[0] : cars[1];
}
回答by Homyk
const example = () => {
var cars = [{color:'red',reg:'111'},{color:'blue',reg:'666'}];
return (cars[0].color === 'red' && cars[0]) ||
cars[1];
}