首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >javascript - dry -如何嵌套

javascript - dry -如何嵌套
EN

Stack Overflow用户
提问于 2020-02-21 16:03:47
回答 6查看 63关注 0票数 1

我是编程新手,但一直在阅读DRY (不要重复自己)代码。

我有一个JavaScript if/else语句,它不适合干方法,但我无法解决如何编写它以使语句不重复。

我希望有比我更聪明的人能教我。

下面是我的代码:

代码语言:javascript
复制
if (response.weather[0].description == "clear sky") {
    const v1 = document.createElement('VIDEO');
    v1.setAttribute("src", "https://static.videezy.com/system/resources/previews/000/011/710/original/Clouds_Timelapse_21_-_60s_-_4k_res.mp4");
    v1.setAttribute("class", "v1")
    document.body.appendChild(v1)
    v1.autoplay = true;
    v1.load();
} else if (response.weather[0].description == "few clouds") {
    v1 = document.createElement('VIDEO');
    v1.setAttribute("src", "https://static.videezy.com/system/resources/previews/000/003/501/original/cloudysky.mp4");
    v1.setAttribute("class", "v1")
    document.body.appendChild(v1)
    v1.autoplay = true;
    v1.load();
}
EN

回答 6

Stack Overflow用户

发布于 2020-02-21 16:15:25

您可以为URL声明一个urls配置以完全消除if/else语句的使用,还可以声明一个loadVideo函数以避免代码重复。

以下是解决方案:

代码语言:javascript
复制
// configure your URLs by weather (using the weather description in this case)
const urls = {
    'clear sky': 'https://static.videezy.com/system/resources/previews/000/011/710/original/Clouds_Timelapse_21_-_60s_-_4k_res.mp4',
    'few clouds': 'https://static.videezy.com/system/resources/previews/000/003/501/original/cloudysky.mp4'
};

// function that loads the video based on a URL
function loadVideo(url) {
    const v1 = document.createElement('VIDEO');
    v1.setAttribute('src', url);
    v1.setAttribute('class', 'v1');
    document.body.appendChild(v1);
    v1.autoplay = true;
    v1.load();
}

// call the function with the response from your API
loadVideo(urls[response.weather[0].description]);
票数 1
EN

Stack Overflow用户

发布于 2020-02-21 16:07:50

您需要在if语句之外提取公共部分:

代码语言:javascript
复制
let v1 = document.createElement('VIDEO');
v1.setAttribute("class", "v1")

if (response.weather[0].description == "clear sky") {
    v1.setAttribute("src", "https://static.videezy.com/system/resources/previews/000/011/710/original/Clouds_Timelapse_21_-_60s_-_4k_res.mp4");
} else if (response.weather[0].description == "few clouds") {
    v1.setAttribute("src", "https://static.videezy.com/system/resources/previews/000/003/501/original/cloudysky.mp4");
}
document.body.appendChild(v1)
v1.autoplay = true;
v1.load();
票数 0
EN

Stack Overflow用户

发布于 2020-02-21 16:08:54

将你的代码分解到一个单独的函数中。

代码语言:javascript
复制
function addVideo(url) {
    const v1 = document.createElement('VIDEO');
    v1.setAttribute("src", url);
    v1.setAttribute("class", "v1")
    document.body.appendChild(v1)
    v1.autoplay = true;
    v1.load();
}

switch (response.weather[0].description) {
    case "clear sky": 
        addVideo("https://static.videezy.com/system/resources/previews/000/011/710/original/Clouds_Timelapse_21_-_60s_-_4k_res.mp4");
        break;
    case "few clouds": 
        addVideo("https://static.videezy.com/system/resources/previews/000/003/501/original/cloudysky.mp4");
        break;
}

或者,您可以使用普通对象将描述值映射到switch,并消除URL。如果你有几个视频,这尤其可取,因为这将比第一种方法更DRYer。

代码语言:javascript
复制
function addVideo(url) {
    const v1 = document.createElement('VIDEO');
    v1.setAttribute("src", url);
    v1.setAttribute("class", "v1")
    document.body.appendChild(v1)
    v1.autoplay = true;
    v1.load();
}

var descriptionVideoMapping = {
    "clear sky": "https://static.videezy.com/system/resources/previews/000/011/710/original/Clouds_Timelapse_21_-_60s_-_4k_res.mp4",
    "few clouds": "https://static.videezy.com/system/resources/previews/000/003/501/original/cloudysky.mp4",
};

var url = descriptionVideoMapping[response.weather[0].description];

if (url) {
    addVideo(url);
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60334419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档