使用这11个代码,可以大大地简化我们的代码

开发 前端
在这篇文章中,我将与你分享一些关于JS的技巧,可以提高你的JS技能。希望能够帮助到你。

[[435521]]

 在这篇文章中,我将与你分享一些关于JS的技巧,可以提高你的JS技能。

1.避免if过长

如果判断值满足多个条件,我们可能会这么写:

  1. if (value === 'a' || value === 'b' || value === 'c') { ... } 

像这样如果有多个条件,if 条件就会很我,可读性降低,我们可以这样简化:

  1. if (['a''b''c'].includes(value)) { ... } 

2.双!操作符将任何变量转换为布尔值

!(NOT)运算符可以使用两次!!,这样可以将任何变量转换为布尔值(像布尔函数),当你需要在处理它之前检查某个值时非常方便。

  1. const toto = null 
  2.  
  3. !!toto // false 
  4. Boolean(toto) // false 
  5.  
  6. if (!!toto) { } // toto is not null or undefined 

3.可选项 (?)

在 JS 中,我们需要经常检查对象的某些属性是否存在,然后才能再处理它,不然会报错。早期我们可能会这么干:

  1. const toto = { a: { b: { c: 5 } } } 
  2.  
  3. if (!!toto.a && !!toto.a.b && !!toto.a.b.c) { ... } // toto.a.b.c exist 

如果对象嵌套很深,我们这写法就难以阅读,这时可以使用?来简化:

  1. if (!!toto.a?.b?.c) { ... } // toto.a.b.c exist 
  2.  
  3. //  如果键不存在,返回 `undefined`。 
  4. const test = toto.a?.b?.c?.d // undefined 

4. 如果if中返回值时, 就不要在写else

经常会看到这种写法:

  1. if (...) { 
  2.   return 'toto' 
  3. else { 
  4.   return 'tutu' 

 如果if有返回值了,可以这样写:

  1. if (...) { 
  2.   return 'toto' 
  3.  
  4. return 'tutu' 

5.避免forEach,多使用filter、map、reduce、every、some

作为初学者,我们使用了很多forEach函数,但 JS 为我们提供了很多选择,而且这些函数是FP(函数式编程)。

filter

filter() 方法创建一个新数组, 其包含通过所提供函数实现的测试的所有元素。

  1. const toto = [1, 2, 3, 4] 
  2.  
  3. // 过滤奇数 
  4. const evenValue = toto.filter(currentValue => { 
  5.    return currentValue % 2 == 0 
  6. }) // [2, 4] 

map

map() 方法创建一个新数组,其结果是该数组中的每个元素是调用一次提供的函数后的返回值。

  1. const toto = [1, 2, 3, 4] 
  2.  
  3. const valueMultiplied = toto.map(currentValue => { 
  4.    return currentValue * 2  
  5. }) // [2, 4, 6, 8] 

reduce

reduce() 方法对数组中的每个元素执行一个由您提供的reducer函数(升序执行),将其结果汇总为单个返回值。

  1. const toto = [1, 2, 3, 4] 
  2.  
  3. const sum = toto.reduce((accumulator, currentValue) => { 
  4.    return accumulator += currentValue  
  5. }, 0) // 10 

Some & Every

some() 方法测试数组中是不是至少有1个元素通过了被提供的函数测试。它返回的是一个Boolean类型的值。

every() 方法测试一个数组内的所有元素是否都能通过某个指定函数的测试。它返回一个布尔值。

什么时候使用?

所有项目都符合一个条件可以用 every

  1. const toto = [ 2, 4 ] 
  2.  
  3. toto.every(val => val % 2 === 0) // true 
  4.  
  5. const falsyToto = [ 2, 4, 5 ] 
  6.  
  7. falsyToto.every(val => val % 2 === 0) // false 

只要一个符合条件就行,用some

  1. const toto = [ 2, 4, 5 ] 
  2.  
  3. toto.some(val => val % 2 !== 0) // return true 

6.不要使用delete来删除属性

从一个对象中 delete 一个属性是非常不好的(性能不好),此外,它还会产生很多副作用。

但是如果你需要删除一个属性,你应该怎么做?

可以使用函数方式创建一个没有此属性的新对象,如下所示:

  1. const removeProperty = (target, propertyToRemove) => { 
  2.   const { [propertyToRemove]: _, ...newTarget } = target 
  3.   return newTarget 
  4. const toto = { a: 55, b: 66 } 
  5. const totoWithoutB = removeProperty(toto, 'b') // { a: 55 } 

7.仅当对象存在时才向其添加属性

有时,如果对象已经定义了属性,我们需要向对象添加属性,我们可能会这样写:

  1. const toto = { name'toto' } 
  2. const other = { other: 'other' } 
  3. // The condition is not important 
  4. const condition = true 
  5.  
  6. if (condition) { 
  7.    other.name = toto.name  

❌不是很好的代码

✅ 可以用一些更优雅的东西!

  1. const condition = true 
  2.  
  3. const other = { 
  4.    other: 'other'
  5.    ...condition && { name'toto' } 

8. 使用模板字符串

在 JS 中学习字符串时,我们需要将它们与变量连接起来

  1. const toto = 'toto' 
  2. const message = 'hello from ' + toto + '!' // hello from toto! 

 如果还有其它变量,我们就得写很长的表达式,这时可以使用模板字符串来优化。

  1. const toto = 'toto' 
  2. const message = `hello from ${toto}!` // hello from toto! 

9. 条件简写

当条件为 true 时,执行某些操作,我们可能会这样写:

  1. if(condition){ 
  2.     toto() 

这种方式可以用 && 简写:

  1. condition && toto() 

10.设置变量的默认值

如果需要给一个变量设置一个默认值,可以这么做:

  1. let toto 
  2.  
  3. console.log(toto) //undefined 
  4.  
  5. toto = toto ?? 'default value' 
  6.  
  7. console.log(toto) //default value 
  8.  
  9. toto = toto ?? 'new value' 
  10.  
  11. console.log(toto) //default value 

11.使用 console timer

如果需要知道一个函数的执行时间,可以这么做:

  1. for (i = 0; i < 100000; i++) { 
  2.   // some code 
  3. console.timeEnd() // x ms 

作者:CodeOz 译者:前端小智 来源:dev 原文:https://dev.to/codeoz/improve-your-js-skls-with-theses-tips-52ia

 

责任编辑:姜华 来源: 今日头条
相关推荐

2020-08-28 10:22:26

前端布局效率

2021-09-09 08:23:11

Vue 技巧 开发工具

2021-03-10 09:20:31

await代码前端

2019-11-08 09:20:57

代码开发工具

2009-07-22 07:47:00

Scala客户代码

2020-08-04 06:32:21

JavaScript代码开发

2020-04-29 14:50:40

代码对比工具

2018-10-18 11:20:06

编程语言代码编码秘诀

2022-11-28 23:44:26

JavaScript技巧程序员

2017-04-19 08:47:42

AsyncJavascript异步代码

2021-12-10 10:21:35

云技术云计算混合云

2014-09-04 13:17:31

2021-05-26 08:50:37

JavaScript代码重构函数

2022-10-20 15:16:23

JavaScript数组技能

2023-02-15 08:30:05

2009-06-04 09:15:45

JavaME手机程序认Java手机

2009-07-06 15:20:30

JSP表达式

2022-02-25 15:59:20

人工智能

2022-02-14 09:12:00

无代码低代码开发工具

2023-09-25 13:06:36

SpringBoot扩展接口
点赞
收藏

51CTO技术栈公众号