如何使JavaScript 代码库变干净
发表于:2026-10-31 作者:千家信息网编辑
千家信息网最后更新 2026年10月31日,这篇文章给大家介绍如何使JavaScript 代码库变干净,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。1、使用默认参数代替短路或条件默认参数通常比短路更干净。function
千家信息网最后更新 2026年10月31日如何使JavaScript 代码库变干净
这篇文章给大家介绍如何使JavaScript 代码库变干净,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
1、使用默认参数代替短路或条件
默认参数通常比短路更干净。
function SomeMethod(paramThatCanBeUndefined) {
const localValue = paramThatCanBeUndefined || "Default Value"; console.log(localValue) // ...}SomeMethod() // Default ValueSomeMethod("SomeValue") // SomeValue尝试以下方法:
function SomeMethod( console.log(paramThatCanBeUndefined) // ...}SomeMethod() // Default ValueSomeMethod("SomeValue") // SomeValue声明:Falsy值,如'',"",false,null,0,和NaN将不会被默认值替代:
function SomeMethod(paramThatCanBeUndefined = "Default Value") { console.log(paramThatCanBeUndefined) // ...}SomeMethod(null) // will not Default Value, will null InsteadSomeMethod("SomeValue") // SomeValue2、处理多个条件
const conditions = ["Condition 2","Condition String2"];someFunction(str){ if(str.includes("someValue1") || str.includes("someValue2")){ return true }else{ return false }}一种更干净的方法是:
someFunction(str){ const conditions = ["someValue1","someValue2"]; return conditions.some(condition=>str.includes(condition));}3、用动态键值对替换开关(即对象文字)
开关版本(或将开关替换为if / else):
const UserRole = { ADMIN: "Admin", GENERAL_USER: "GeneralUser", SUPER_ADMIN: "SuperAdmin",};function getRoute(userRole = "default role"){ switch(userRole){ case UserRole.ADMIN: return "/admin" case UserRole.GENERAL_USER: return "/GENERAL_USER" case UserRole.SUPER_ADMIN: return "/superadmin" default: return "/" }}console.log(getRoute(UserRole.ADMIN)) // return "/admin"console.log(getRoute("Anything")) // return Default pathconsole.log(getRoute()) // return Default pathconsole.log(getRoute(null)) // return Default path// More cases if new arrive// You can think if else instead of switch动态键值对版本:
const UserRole = { ADMIN: "Admin", GENERAL_USER: "GeneralUser", SUPER_ADMIN: "SuperAdmin",};function getRoute(userRole = "default role"){ const appRoute = { [UserRole.ADMIN]: "/admin", [UserRole.GENERAL_USER]: "/user", [UserRole.SUPER_ADMIN]: "/superadmin" }; return appRoute[userRole] || "Default path";}console.log(getRoute(UserRole.ADMIN)) // return "/admin"console.log(getRoute("Anything")) // return Default pathconsole.log(getRoute()) // return Default pathconsole.log(getRoute(null)) // return Default path// No more switch/if-else here.// Easy to Further expansion4、避免过多的函数参数
function myFunction(employeeName,jobTitle,yrExp,majorExp){ return `${employeeName} is working as ${jobTitle} with ${yrExp} years of experience in ${majorExp}`}//output be like John is working as Project Manager with 12 year of experience in Project Management// you can call it viaconsole.log(myFunction("John","Project Manager",12,"Project Management"))// ***** PROBLEMS ARE *****// Violation of 'clean code' principle// Parameter sequencing is important// Unused Params warning if not used// Testing need to consider a lot of edge cases.这是一种更清洁的方法:
function myFunction({employeeName,jobTitle,yrExp,majorExp}){ return `${employeeName} is working as ${jobTitle} with ${yrExp} years of experience in ${majorExp}`}//output be like John is working as Project Manager with 12 year of experience in Project Management// you can call it viaconst mockTechPeople = { employeeName:"John", jobTitle:"Project Manager", yrExp:12, majorExp:"Project Management"}console.log(myFunction(mockTechPeople))// ES2015/ES6 destructuring syntax is in action// map your desired value to variable you need.5、使用Object.assign设置默认对象
这看起来很繁琐:
const someObject = { title: null, subTitle: "Subtitle", buttonColor: null, disabled: true};function createOption(someObject) { someObject.title = someObject.title || "Default Title"; someObject.subTitle = someObject.subTitle || "Default Subtitle"; someObject.buttonColor = someObject.buttonColor || "blue"; someObject.disabled = someObject.disabled !== undefined ? someObject.disabled : true; return someObject}console.log(createOption(someObject));// Output be like // {title: 'Default Title', subTitle: 'Subtitle', buttonColor: 'blue', disabled: true}这种方法看起来更好:
const someObject = { title: null, subTitle: "Subtitle", buttonColor: null, disabled: true }; function creteOption(someObject) { const newObject = Object.assign({ title: "Default Title", subTitle: "Default Subtitle", buttonColor: "blue", disabled: true },someObject) return newObject } console.log(creteOption(someObject));关于如何使JavaScript 代码库变干净就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
干净
方法
参数
代码
内容
动态
对象
更多
条件
版本
帮助
不错
繁琐
兴趣
函数
多个
小伙
小伙伴
文字
文章
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
互联网科技社
互联网科技公司发展
中国互联网科技发展
互联网女皇 腾讯科技
互联网科技股有哪些
互联网科技公司需要的职位
新华互联网科技2019
科技互联网的估值
茂名市互联网科技产业园
重庆互联网医院沙龙医盟信息科技
996高科技互联网公司
深圳艾果互联网通讯科技
中和天地互联网科技有限公司
陕西云淼互联网科技公司
广州互联网高科技取餐柜
济南小医互联网科技有限公司招聘
版纳互联网科技选哪家
上海万顺科技互联网有限公司招聘
第三届互联网大会安防黑科技
承德科技产业互联网平台
互联网科技公司经营范围
金融科技还是互联网
互联网品科技
关于新基建和互联网科技
互联网科技小贷
广州互联网络科技有限公司
唐信互联网科技 大连
天速互联网络科技有限公司
天津腾达互联网科技有限公司
世界互联网领先科技成果获奖