自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

五種使 JavaScript 代碼庫更干凈的方法

開發(fā) 前端
今天向大家介紹5種使JavaScript代碼庫更干凈的方法,一起來看一下都有哪些吧!

 [[442863]]

1、使用默認參數(shù)代替短路或條件

默認參數(shù)通常比短路更干凈。

 

  1. function SomeMethod(paramThatCanBeUndefined) { 
  2.  
  3.    const localValue = paramThatCanBeUndefined || "Default Value"
  4.    console.log(localValue) 
  5.    // ... 
  6. SomeMethod() // Default Value 
  7. SomeMethod("SomeValue") // SomeValue 

 

嘗試以下方法:

 

  1. function SomeMethod( 
  2.   console.log(paramThatCanBeUndefined) 
  3.   // ... 
  4. SomeMethod() // Default Value 
  5. SomeMethod("SomeValue") // SomeValue 

 

聲明:Falsy值,如'',"",false,null,0,和NaN將不會被默認值替代:

 

  1. function SomeMethod(paramThatCanBeUndefined = "Default Value") {         
  2.   console.log(paramThatCanBeUndefined)   
  3.   // ... 
  4. SomeMethod(null) // will not Default Value, will null Instead 
  5. SomeMethod("SomeValue") // SomeValue 

 

2、處理多個條件

 

  1. const conditions = ["Condition 2","Condition String2"]; 
  2. someFunction(str){ 
  3.   if(str.includes("someValue1") || str.includes("someValue2")){ 
  4.     return true 
  5.   }else
  6.     return false 
  7.   } 

 

一種更干凈的方法是:

 

  1. someFunction(str){ 
  2.    const conditions = ["someValue1","someValue2"]; 
  3.    return conditions.some(condition=>str.includes(condition)); 

 

3、用動態(tài)鍵值對替換開關(即對象文字)

開關版本(或?qū)㈤_關替換為if / else):

 

  1. const UserRole = { 
  2.   ADMIN: "Admin"
  3.   GENERAL_USER: "GeneralUser"
  4.   SUPER_ADMIN: "SuperAdmin"
  5. }; 
  6. function getRoute(userRole = "default role"){ 
  7.  
  8.  
  9.   switch(userRole){ 
  10.     case UserRole.ADMIN: 
  11.       return "/admin" 
  12.     case UserRole.GENERAL_USER: 
  13.         return "/GENERAL_USER" 
  14.     case UserRole.SUPER_ADMIN: 
  15.         return "/superadmin" 
  16.     default
  17.       return "/"  
  18.   } 
  19.  
  20. console.log(getRoute(UserRole.ADMIN)) // return "/admin" 
  21. console.log(getRoute("Anything")) // return Default path 
  22. console.log(getRoute()) // return Default path 
  23. console.log(getRoute(null)) // return Default path 
  24.  
  25. // More cases if new arrive 
  26. // You can think if else instead of switch 

 

動態(tài)鍵值對版本:

 

  1. const UserRole = { 
  2.    ADMIN: "Admin"
  3.    GENERAL_USER: "GeneralUser"
  4.    SUPER_ADMIN: "SuperAdmin"
  5. }; 
  6. function getRoute(userRole = "default role"){ 
  7.  const appRoute = { 
  8.   [UserRole.ADMIN]: "/admin"
  9.   [UserRole.GENERAL_USER]: "/user"
  10.   [UserRole.SUPER_ADMIN]: "/superadmin" 
  11.  }; 
  12.  return appRoute[userRole] || "Default path"
  13. console.log(getRoute(UserRole.ADMIN)) // return "/admin" 
  14. console.log(getRoute("Anything")) // return Default path 
  15. console.log(getRoute()) // return Default path 
  16. console.log(getRoute(null)) // return Default path 
  17. // No more switch/if-else here. 
  18. // Easy to Further expansion 

 

4、避免過多的函數(shù)參數(shù)

 

  1. function myFunction(employeeName,jobTitle,yrExp,majorExp){ 
  2.  return `${employeeName} is working as ${jobTitle} with ${yrExp}    years of experience in ${majorExp}` 
  3. //output be like John is working as Project Manager with 12 year of experience in Project Management 
  4. // you can call it via 
  5. console.log(myFunction("John","Project Manager",12,"Project Management")) 
  6. //    ***** PROBLEMS ARE ***** 
  7. // Violation of 'clean code' principle 
  8. // Parameter sequencing is important 
  9. // Unused Params warning if not used 
  10. // Testing need to consider a lot of edge cases. 

 

這是一種更清潔的方法:

 

  1. function myFunction({employeeName,jobTitle,yrExp,majorExp}){ 
  2.  return `${employeeName} is working as ${jobTitle} with ${yrExp} years of experience in ${majorExp}` 
  3. //output be like John is working as Project Manager with 12 year of experience in Project Management 
  4. // you can call it via 
  5. const mockTechPeople = { 
  6.   employeeName:"John"
  7.   jobTitle:"Project Manager"
  8.   yrExp:12, 
  9.   majorExp:"Project Management" 
  10. console.log(myFunction(mockTechPeople)) 
  11. // ES2015/ES6 destructuring syntax is in action 
  12. // map your desired value to variable you need. 

 

5、使用Object.assign設置默認對象

這看起來很繁瑣:

 

  1. const someObject = { 
  2.  title: null
  3.  subTitle: "Subtitle"
  4.  buttonColor: null
  5.  disabled: true 
  6. }; 
  7. function createOption(someObject) { 
  8.  someObject.title = someObject.title || "Default Title"
  9.  someObject.subTitle = someObject.subTitle || "Default Subtitle"
  10.  someObject.buttonColor = someObject.buttonColor || "blue"
  11.  someObject.disabled = someObject.disabled !== undefined ?  someObject.disabled : true
  12.  return someObject 
  13. console.log(createOption(someObject)); 
  14.  
  15. // Output be like  
  16. // {title: 'Default Title', subTitle: 'Subtitle', buttonColor: 'blue', disabled: true

 

這種方法看起來更好:

 

  1. const someObject = { 
  2.   title: null
  3.   subTitle: "Subtitle"
  4.   buttonColor: null
  5.   disabled: true 
  6.  }; 
  7.  function creteOption(someObject) { 
  8.   const newObject = Object.assign({ 
  9.    title: "Default Title"
  10.    subTitle: "Default Subtitle"
  11.    buttonColor: "blue"
  12.    disabled: true 
  13.  },someObject) 
  14.  return newObject 
  15.  } 
  16.  console.log(creteOption(someObject)); 

 

責任編輯:華軒 來源: 今日頭條
相關推薦

2011-07-27 09:41:52

虛擬化

2022-06-07 09:30:35

JavaScript變量名參數(shù)

2020-03-09 19:10:01

AI物聯(lián)網(wǎng)智能

2021-09-01 08:55:20

JavaScript代碼開發(fā)

2021-11-30 10:20:24

JavaScript代碼前端

2020-01-06 10:01:12

JavaScript瀏覽器HTML

2020-02-11 12:35:19

Kubernetes容器

2023-05-09 15:01:43

JavaScript編程語言異常處理

2023-11-21 15:23:15

JavaScript工具

2022-05-10 10:28:21

JavaScript代碼

2023-11-14 13:39:40

2023-03-30 13:59:29

物聯(lián)網(wǎng)工業(yè)4.0

2019-09-23 10:59:31

機器學習算法編程

2019-09-23 11:17:46

機器學習數(shù)據(jù)技術

2023-06-02 15:42:51

JavaScript數(shù)據(jù)結構對象

2019-08-28 10:05:27

數(shù)據(jù)中心云計算互聯(lián)網(wǎng)

2022-03-16 11:06:05

區(qū)塊鏈支付安全

2024-04-22 13:42:32

大型語言模型人工智能

2022-10-31 07:09:15

拷貝代碼項目

2015-05-11 10:48:28

代碼干凈的代碼越少越干凈
點贊
收藏

51CTO技術棧公眾號