前端开发不得不知的ES6十大新特性

栏目: JavaScript · 发布时间: 5年前

内容简介:还记得我们以前(ES5)不得不通过下面方式来定义默认参数:一切工作都是正常的,直到参数值是0后,就有问题了,因为在JavaScript中,0表示false 但在ES6,我们可以直接把默认值放在函数申明里:在其它语言中,使用模板和插入值是在字符串里面输出变量的一种方式。 因此,在ES5,我们可以这样组合一个字符串:
  1. 默认参数 in es6
  2. 模板文本 in es6
  3. 多行字符串 in es6
  4. 解构赋值 in es6
  5. 增强的对象文本 in es6
  6. 箭头函数 in es6
  7. Promise in es6
  8. 块作用域构造Let and Const
  9. Classes 类 in es6
  10. Modules 模块 in es6

1.Default Parameters(默认参数) in ES6

还记得我们以前(ES5)不得不通过下面方式来定义默认参数:

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

一切工作都是正常的,直到参数值是0后,就有问题了,因为在JavaScript中,0表示false 但在ES6,我们可以直接把默认值放在函数申明里:

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

2.Template Literals(模板对象) in ES6

在其它语言中,使用模板和插入值是在字符串里面输出变量的一种方式。 因此,在ES5,我们可以这样组合一个字符串:

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

幸运的是,在ES6中,我们可以使用新的语法$ {NAME},并把它放在反引号里:

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

3.Multi-line Strings (多行字符串)in ES6

ES6的多行字符串是一个非常实用的功能。 在ES5中,我们不得不使用以下方法来表示多行字符串:

var roadPoem = 'Then took the other, as just as fair,nt'
  + 'And having perhaps the better claimnt'
  + 'Because it was grassy and wanted wear,nt'
  + 'Though as for that the passing therent'
  + 'Had worn them really about the same,nt';
    
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.Destructuring Assignment (解构赋值)in ES6

解构可能是一个比较难以掌握的概念。先从一个简单的赋值讲起,其中house 和 mouse是key,同时house 和mouse也是一个变量,在ES5中是这样:

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

在ES6,我们可以使用这些语句代替上面的ES5代码:

// we'll get house and mouse variables
var { house, mouse } = $('body').data();
复制代码

这个同样也适用于数组,非常赞的用法:

var [col1, col2]  = $('.column');
var [line1, line2, line3, , line5] = file.split('n');
复制代码

6.Arrow Functions in(箭头函数) ES6

例如,下面的代码用ES5就不是很优雅:

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

在ES6中就不需要用 _this = this:

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

7. Promises in ES6

下面是一个简单的用setTimeout()实现的异步延迟加载函数:

setTimeout(function() {
  console.log('Yay!');
}, 1000);
复制代码

在ES6中,我们可以用promise重写:

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

到目前为止,代码的行数从三行增加到五行,并没有任何明显的好处。确实,如果我们有更多的嵌套逻辑在setTimeout()回调函数中,我们将发现更多好处:

setTimeout(function(){
  console.log('Yay!');
  setTimeout(function(){
    console.log('Wheeyee!');
  }, 1000);
}, 1000);
复制代码

在ES6中我们可以用promises重写:

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

以上所述就是小编给大家介绍的《前端开发不得不知的ES6十大新特性》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

函数响应式领域建模

函数响应式领域建模

【美】Debasish Ghosh / 李源 / 电子工业出版社 / 2018-1 / 79

传统的分布式应用不会切入微服务、快速数据及传感器网络的响应式世界。为了捕获这些应用的动态联系及依赖,我们需要使用另外一种方式来进行领域建模。由纯函数构成的领域模型是以一种更加自然的方式来反映一个响应式系统内的处理流程,同时它也直接映射到了相应的技术和模式,比如Akka、CQRS 以及事件溯源。《函数响应式领域建模》讲述了响应式系统中建立领域模型所需要的通用且可重用的技巧——首先介绍了函数式编程和响......一起来看看 《函数响应式领域建模》 这本书的介绍吧!

随机密码生成器
随机密码生成器

多种字符组合密码

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具