javascript - 是否可以使用对象而不是 Switch 语句?
问题描述
我经常在我的代码中使用 switch 语句,我读到了这个对象,很多开发人员说它更好用。我想使用更少的代码,你们知道使这段代码更高效的替代方法吗?
完整的代码链接在这里 - https://jsfiddle.net/lmanhaes/5ebjypo1/
我在下面分享了两个我想更改的示例。
非常感谢。
//fisrt example
$(document).ready(function () {
$("#country").change(function () {
var cityName;
var select = $("#country option:selected").val();
switch (select) {
case "England":
cityName = "england";
city(cityName);
break;
case "Scotland":
cityName = "scotland";
city(cityName);
break;
case "Wales":
cityName = "wales";
city(cityName);
break;
case "Northern Ireland":
cityName = "nireland";
city(cityName);
break;
default:
$("#city").empty();
break;
}
});
$("#city").change(function () {
var cityValue = $("#city").val();
getWeatherApi(cityValue);
});
//second example
$.each(response.weather, function (index, value) {
cond = response.weather[index].main;
switch (cond) {
case "Clouds":
icon = '<img src="./weather_icons/cloud.png" alt="cloud" width="70px" height="80%"/>';
break;
case "Hail":
icon = '<img src="./weather_icons/hail.png" alt="hail" width="70px" height="80%"/>';
break;
case "Heavy Cloud":
icon = '<img src="./weather_icons/heavy cloud.png" alt="heavy-clouds" width="70px" height="80%"/>';
break;
case "Heavy Rain":
icon = '<img src="./weather_icons/heavy rain.png" alt="heavy-rain" width="70px" height="80%"/>';
break;
case "Rain":
icon = '<img src="./weather_icons/rain.png" alt="rain" width="70px" height="80%"/>';
break;
case "Sleet":
icon = '<img src="./weather_icons/sleet.png" alt="sleet" 70px" height="80%"/>';
break;
case "Snow":
icon = '<img src="./weather_icons/snow.png" alt="snow" width="70px" height="80%"/>';
break;
case "Sun":
icon = '<img src="./weather_icons/sun.png" alt="sun" 70px" height="80%"/>';
break;
case "Sun and Clouds":
icon = '<img src="./weather_icons/sun and cloud.png" alt="sun-cloud" width="70px" height="80%"/>';
break
case "Thunderstorm":
icon = '<img src="./weather_icons/thunderstorm.png" alt="thunderstorm" width="70px" height="80%"/>';
break;
default:
icon = '';
break;
}
});
解决方案
对象和 switch-case 块之间有太多的主要区别。对象是安全数据的数据结构,开关块是缩短 if-else 结构的句法结构。对于内存和 cpu 性能,请使用 switch-case 语句。
为了更好地概述代码:在您的案例块中查找重复项。您还可以在自己的函数中外包您的 switch 语句。有关更多概述,请使用干净的功能名称并将此类功能外包给模块。
例如:
$(document).ready(function () {
$("#country").change(function () {
var cityName;
var select = $("#country option:selected").val();
var convertedCityName = convertCityNames(select);
if(convertedCityName) {
city(convertCityNames);
} else {
$("#city").empty();
}
});
function convertCityNames(inputedCityName) {
switch (inputedCityName) {
case "England":
return "england";
case "Scotland":
return "scotland";
case "Wales":
return "wales";
case "Northern Ireland":
return "nireland";
default:
return;
}
}
推荐阅读
- c# - 如何在 SQL Server 中搜索乌尔都语文本?
- javascript - Cloud Firestore 未在通配符中触发 onWrite
- android - Android-如何使用自定义名称保存完整照片?
- vue.js - 如何在 Chart JS 中的饼图标签上避免 NaN
- angular - 如何更改 ElectronJS 应用程序的默认图标?
- php - Xdebug 应该减慢每个请求的速度吗?
- python - 列表值替换未按预期工作
- android - 如何编写firebase安全规则,例如如果用户发布帖子然后只有他和管理员可以写入其余用户只能阅读它?
- python - 将数据连续添加到python中的csv文件
- javascript - 无法从 VueJS 中的 props 中 require() 变量路径