当前位置:网站首页>分享 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
边栏推荐
- 线程同步、生命周期
- 面试官:说一下类加载的过程以及类加载的机制(双亲委派机制)
- L'externalisation a duré quatre ans.
- Find daffodils - for loop practice
- LeetCode162-寻找峰值-二分-数组
- A blog allows you to learn how to write markdown on vscode
- QT interface optimization: double click effect
- DVWA之暴力破解(Brute Force)Low-->high
- 【无标题】
- Explanation and example application of the principle of logistic regression in machine learning
猜你喜欢
Swift - literal, literal protocol, conversion between basic data types and dictionary / array
AT89C52 MCU frequency meter (1Hz ~ 20MHz) design, LCD1602 display, including simulation, schematic diagram, PCB and code, etc
Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
On the insecurity of using scanf in VS
外包干了四年,废了...
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
[detailed explanation of factory mode] factory method mode
A good tool: aardio
你還不知道責任鏈模式的使用場景嗎?
随机推荐
capacitance
机器学习之逻辑回归(Logistic Regression)原理讲解和实例应用,果断收藏
Swift: entry of program, swift calls OC@_ silgen_ Name, OC calls swift, dynamic, string, substring
每日一题-LeetCode396-旋转函数-递推
牛客网数据库SQL实战详细剖析(26-30)
拼接hql时,新增字段没有出现在构造方法中
AT89C51 MCU digital voltmeter development, measuring range 0 ~ 5V, proteus simulation, schematic diagram, PCB and C program, etc
[untitled]
do(Local scope)、初始化器、内存冲突、Swift指针、inout、unsafepointer、unsafeBitCast、successor、
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
QT actual combat: Yunxi calendar
we引用My97DatePicker 实现时间插件使用
Leetcode153 - find the minimum value in the rotation sort array - array - binary search
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
Raised exception class eaccexviolation with 'access violation at address 45efd5 in module error
[proteus simulation] automatic range (range < 10V) switching digital voltmeter
[NLP] HMM hidden Markov + Viterbi word segmentation
Vscode Chinese plug-in doesn't work. Problem solving
Chapter 7 of JVM series -- bytecode execution engine
1 minute to understand the execution process and permanently master the for cycle (with for cycle cases)