wangkiliaosi8899 最近的时间轴更新
wangkiliaosi8899

wangkiliaosi8899

V2EX 第 465679 号会员,加入于 2020-01-18 00:17:37 +08:00
wangkiliaosi8899 最近回复了
2020-01-18 17:08:00 +08:00
回复了 IMFFA 创建的主题 JavaScript 请教一下,怎么把这段代码换一个优雅的写法
看样子楼主深谙命令式编程的精髓;

function resolve(value){
const time = ['日','周','半月','月','季','半年','年'];
if(!Number.isInteger(value) || value>time.length){
throw new Error(`参数错误:需要 1~7 的整数`);
}
return Array.from(Array(time.length)).reduce((result, _c, index)=>{
const template = `<input
id="cycle_type_${index}"
type="radio"
name="cycle_type"
value="${index+1}"
${index+1===value?"checked":''}
title="${time[index]}"
lay-filter="cycle_type">`;
return result+template
},'')
}

console.log(resolve(2));
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   944 人在线   最高记录 6543   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 11ms · UTC 21:09 · PVG 05:09 · LAX 14:09 · JFK 17:09
Developed with CodeLauncher
♥ Do have faith in what you're doing.