博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
最最最常用的十大ES6特性总结
阅读量:6837 次
发布时间:2019-06-26

本文共 3145 字,大约阅读时间需要 10 分钟。

1. 默认参数

之前我们使用的默认传参方式:

var link = function (height, color, url) {    var height = height || 50    var color = color || 'red'    var url = url || 'http://azat.co'    ...}复制代码

现在es6新语法:

var link = function(height = 50, color = 'red', url = 'http://azat.co') {  ...}复制代码

这个不用解释,简单易懂!

2. 字符串拼接

之前我们使用的字符串拼接方式:

var name = 'Your name is ' + first + ' ' + last + '.';var url = 'http://localhost:3000/api/messages/' + id;复制代码

现在es6新语法:

var name = `Your name is ${first} ${last}.`;var url = `http://localhost:3000/api/messages/${id}`;复制代码

使用``反引号,位置在tab的上方,应该也简单易懂!

3. 换行字符串拼接

之前我们使用的字符串拼接方式:

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'var fourAgreements = 'You have the right to be you.\n\    You can only be you when you do your best.'复制代码

现在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 as for that the passing there    Had worn them really about the same,`var fourAgreements = `You have the right to be you.    You can only be you when you do your best.`复制代码

同样使用``反引号,就不用管换行了!

4. 结构赋值

之前我们使用的赋值方式:

var data = $('body').data(), // data has properties house and mouse  house = data.house,  mouse = data.mouse复制代码

现在es6新语法:

var {house, mouse} = $('body').data() // we'll get house and mouse variablesvar {
json: jsonMiddleware} = require('body-parser')var {username, password} = req.bodyvar [col1, col2] = $('.column'), [line1, line2, line3, , line5] = file.split('\n')复制代码

5. 对象加强

之前我们使用方式:

var serviceBase = {
port: 3000, url: 'azat.co'}, getAccounts = function(){
return [1,2,3]}var accountServiceES5 = { port: serviceBase.port, url: serviceBase.url, getAccounts: getAccounts, toString: function() { return JSON.stringify(this.valueOf()) }, getUrl: function() {
return "http://" + this.url + ':' + this.port}, valueOf_1_2_3: getAccounts()}复制代码

现在es6新语法:

var accountServiceES5ObjectCreate = Object.create(serviceBase)var accountServiceES5ObjectCreate = {  getAccounts: getAccounts,  toString: function() {    return JSON.stringify(this.valueOf())  },  getUrl: function() {
return "http://" + this.url + ':' + this.port}, valueOf_1_2_3: getAccounts()}复制代码

现在可以继承了!

6. 箭头函数

之前我们使用方式:

var _this = this$('.btn').click(function(event){  _this.sendData()})复制代码

现在es6新语法:

$('.btn').click((event) =>{  this.sendData()})复制代码

一个箭头就搞定了!

7. Promises

现在es6新语法:

var wait1000 =  new Promise(function(resolve, reject) {  setTimeout(resolve, 1000)}).then(function() {  console.log('Yay!')})复制代码

只是语法上面简单些,更加容易阅读和维护!

8. Let 和 Const

现在es6新语法:

for (var i = 0; i < 10; i++) {}    console.log(i); //10for (let j = 0; j < 10; j++) {}console.log(j); //Uncaught ReferenceError: j is not definedconst value = 100;console.log(value);value = 200; //Uncaught TypeError: Assignment to constant variable.复制代码

letvar比较,let限制了作用域,const不能被改变

9. class类

这里东西比较多,后续更新

10. 模块化

这里东西比较多,后续更新

转载于:https://juejin.im/post/5afd1ac451882542c760cb26

你可能感兴趣的文章
python 第一课
查看>>
微信搜索谁把你删除了
查看>>
1482奖学金
查看>>
虚拟化数据中心服务器硬件配置建议
查看>>
CentOS6.4 添加播放×××
查看>>
支付宝移动支付文档url
查看>>
自动化运维之CentOS7下PXE+Kickstart+DHCP+TFTP+HTTP无人值守安装系统
查看>>
SFB 项目经验-51-某上市企业2千人Exchange 2013升级2016高可用之伤01
查看>>
【迁移2018-04-12 10:46:11】BeanCopier之MapStruct(一)
查看>>
编程的智慧
查看>>
spring 项目中的一个异常
查看>>
16个Linux服务器监控命令
查看>>
我的友情链接
查看>>
CentOS PPTP ×××
查看>>
电子工程师必须知道的10个网站 !!!
查看>>
我的友情链接
查看>>
防Xss攻击,包含富文本编辑器的处理
查看>>
MyBatis延迟加载
查看>>
利用MAVEN打包可运行jar包,包括依赖的第三方包
查看>>
Java调用 shell脚本阻塞
查看>>