ES6,正式名称是ECMAScript2015,然则ES6这个名称加倍简洁。ES6已经不再是JavaScript最新的标准,然则它已经广泛用于编程实践中。如不雅你还没用过ES6,如今还不算太晚...
下面是10个ES6最佳特点,排名不分先后:
- 函数参数默认值
- 模板字符串
- 多行字符串
- 解构赋值
- 对象属性简写
- 箭头函数
- Promise
- Let与Const
- 类
- 模块化
不应用ES6
为函数的参数设置默认值:
- function foo(height, color)
- {
- var height = height || 50;
- var color = color || 'red';
- //...
- }
如许写一般没问题,然则,当参数的布尔值为false时,是会出工作的!比如,我们如许调用foo函数:
应用ES6
- foo(0, "", "")
- function foo(height = 50, color = 'red')
- {
- // ...
- }
应用两个then是异步编程串行化,避免了回调地狱:
不应用ES6
应用+号将变量拼接为字符串:
module.js中应用export导出port变量和getAccounts函数:
- var name = 'Your name is ' + first + ' ' + last + '.'
应用ES6
- var name = `Your name is ${first} ${last}.`
ES6的写法加倍简洁、直不雅。
3. 多行字符串
不应用ES6
应用"nt"将多行字符串拼接起来:
- var roadPoem = 'Then took the other, as just as fair,\n\t'
- + 'And having perhaps the better claim\n\t'
- + 'Because it was grassy and wanted wear,\n\t'
- + 'Though as for that the passing there\n\t'
- + 'Had worn them really about the same,\n\t'
应用ES6
将多行字符串放在反引号``之间就好了:
- var roadPoem = `Then took the other, as just as fair,
- And having perhaps the better claim
- Because it was grassy and wanted wear,
- Though
推荐阅读
一、经由过程 show status 敕令懂得各类 sql 的履行频率show status 敕令中心可以参加选项 session(默认) 或 global: session (当前连接) global (自数据前次启动至今)# Com_xxx 表>>>详细阅读
本文标题:关于ES6的10个最佳特性
地址:http://www.17bianji.com/lsqh/37056.html
1/2 1