当前位置:网站首页>分享 20 个不容错过的 ES6 的技巧
分享 20 个不容错过的 ES6 的技巧
2022-04-23 14:49:00 【前端达人】
前言
大家好,我是 xieyezi,好久不见,我又重新回归掘金啦, 这次为大家整理了20个使用频率很高的ES6代码块,希望大家喜欢
文中代码对应的详细注释和具体使用方法都放在我的
github
上,源代码在底部连接
1. 打乱数组顺序

2. 去除数字之外的所有字符

3. 反转字符串或者单词

4. 将十进制转换为二进制或十六进制

5. 合并多个对象

6. ===
和 ==
的区别

7. 解构赋值

8. 交换变量的值

9-1. 判断回文字符串

回文字符串: 正着写和反着写都一样的字符串 (特别感谢\@浮生阁阁主[1]勘误)
9-2 判断两个字符串是否为互相排列

判断两个字符串是否为互相排列: 给定两个字符串,一个是否是另一个的排列
10. 可选链操作符

MDN: 可选链操作符(
?.
)允许读取位于连接对象链深处的属性的值,而不必明确验证链中的每个引用是否有效。?.
操作符的功能类似于.
链式操作符,不同之处在于,在引用为空(nullish ) (null
或者undefined
) 的情况下不会引起错误,该表达式短路返回值是undefined
。与函数调用一起使用时,如果给定的函数不存在,则返回undefined
例如:
if (res && res.data && res.data.success) {
//code
}
相当于:
if (res?.data?.success) {
// code
}
11. 三目运算符

12. 从数组中随机选择一个值

13. 冻结对象

14. 删除数组重复的元素

15. 保留指定位小数

16. 清空数组

17. 从 RGB
转换为 HEX

18. 从数组中获取最大值和最小值

19. 空值合并运算符

MDN: 空值合并操作符(
??
)是一个逻辑操作符,当左侧的操作数为null
或者undefined
时,返回其右侧操作数,否则返回左侧操作数。
20. 过滤数组中值为 false
的值

源码
20个不容错过的ES6技巧[2]
以上,码字作图很辛苦,还望不要吝啬手中的赞,你的点赞是我继续更新的最大动力!
关于本文
作者:Xieyezi
https://juejin.cn/post/7083145771461115941
版权声明
本文为[前端达人]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Ed7zgeE9X/article/details/124357649
边栏推荐
- do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
- Arduino for esp8266串口功能简介
- Select receives both normal data and out of band data
- 如何打开Win10启动文件夹?
- [stc8g2k64s4] introduction of comparator and sample program of comparator power down detection
- [detailed explanation of factory mode] factory method mode
- Want to be an architect? Tamping the foundation is the most important
- 面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
- How do I open the win10 startup folder?
- Mq-2 and DS18B20 fire temperature smoke alarm system design, 51 single chip microcomputer, with simulation, C code, schematic diagram, PCB, etc
猜你喜欢
随机推荐
Swift protocol Association object resource name management multithreading GCD delay once
PCIe X1 插槽的主要用途是什么?
如何打开Win10启动文件夹?
MySQL error packet out of order
capacitance
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
Svn detailed use tutorial
555 timer + 74 series chip to build eight way responder, 30s countdown, proteus simulation, etc
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
利用 MATLAB 编程实现最速下降法求解无约束最优化问题
LeetCode149-直线上最多的点数-数学-哈希表
《JVM系列》 第七章 -- 字节码执行引擎
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
1n5408-asemi rectifier diode
Leetcode149 - maximum number of points on a line - Math - hash table
成都控制板设计提供_算是详细了_单片机程序头文件的定义、编写及引用介绍
[NLP] HMM hidden Markov + Viterbi word segmentation
The art of automation
A good tool: aardio
thinkphp5+数据大屏展示效果