译者按: 人生苦短,我用ES6。javascript
原文: Top 10 ES6 Features Every Busy JavaScript Developer Must Knowhtml
译者: Fundebug前端
为了保证可读性,本文采用意译而非直译,而且对源代码进行了大量修改。另外,本文版权归原做者全部,翻译仅用于学习。java
ES6,正式名称是ECMAScript2015,可是ES6这个名称更加简洁。ES6已经再也不是JavaScript最新的标准,可是它已经普遍用于编程实践中。若是你还没用过ES6,如今还不算太晚...git
下面是10个ES6最佳特性,排名不分前后:es6
为函数的参数设置默认值:github
function foo(height, color) { var height = height || 50; var color = color || 'red'; //... }
这样写通常没问题,可是,当参数的布尔值为false时,是会出事情的!好比,咱们这样调用foo函数:web
foo(0, "", "")
由于0的布尔值为false,这样height的取值将是50。同理color的取值为'red'。编程
function foo(height = 50, color = 'red') { // ... }
使用+号将变量拼接为字符串:小程序
var name = 'Your name is ' + first + ' ' + last + '.'
将变量放在大括号之中:
var name = `Your name is ${first} ${last}.`
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'
将多行字符串放在反引号``之间就行了:
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 data = $('body').data(); // data有house和mouse属性 var house = data.house; var mouse = data.mouse;
一次性获取对象的子属性:
var { house, mouse} = $('body').data()
对于数组也是同样的:
var [col1, col2] = $('.column');
对象中必须包含属性和值,显得很是多余:
var bar = 'bar'; var foo = function () { // ... } var baz = { bar: bar, foo: foo };
对象中直接写变量,很是简单:
var bar = 'bar'; var foo = function () { // ... } var baz = { bar, foo };
普通函数体内的this,指向调用时所在的对象。
function foo() { console.log(this.id); } var id = 1; foo(); // 输出1 foo.call({ id: 2 }); // 输出2
箭头函数体内的this,就是定义时所在的对象,而不是调用时所在的对象。
var foo = () => { console.log(this.id); } var id = 1; foo(); // 输出1 foo.call({ id: 2 }); // 输出1
嵌套两个setTimeout回调函数:
setTimeout(function() { console.log('Hello'); // 1秒后输出"Hello" setTimeout(function() { console.log('Fundebug'); // 2秒后输出"Fundebug" }, 1000); }, 1000);
使用两个then是异步编程串行化,避免了回调地狱:
var wait1000 = new Promise(function(resolve, reject) { setTimeout(resolve, 1000); }); wait1000 .then(function() { console.log("Hello"); // 1秒后输出"Hello" return wait1000; }) .then(function() { console.log("Fundebug"); // 2秒后输出"Fundebug" });
var定义的变量未函数级做用域:
{ var a = 10; } console.log(a); // 输出10
let定义的变量为块级做用域,所以会报错:(若是你但愿实时监控JavaScript应用的错误,欢迎无偿使用Fundebug)
{ let a = 10; } console.log(a); // 报错“ReferenceError: a is not defined”
const与let同样,也是块级做用域。
使用构造函数建立对象:
function Point(x, y) { this.x = x; this.y = y; this.add = function() { return this.x + this.y; }; } var p = new Point(1, 2); console.log(p.add()); // 输出3
使用Class定义类,更加规范,且你可以继承:
class Point { constructor(x, y) { this.x = x; this.y = y; } add() { return this.x + this.y; } } var p = new Point(1, 2); console.log(p.add()); // 输出3
JavaScript一直没有官方的模块化解决方案,开发者在实践中主要采用CommonJS和AMD规范。而ES6制定了模块(Module)功能。
Node.js采用CommenJS规范实现了模块化,而前端也能够采用,只是在部署时须要使用Browserify等工具打包。这里不妨介绍一下CommenJS规范。
module.js中使用module.exports导出port变量和getAccounts函数:
module.exports = { port: 3000, getAccounts: function() { ... } }
main.js中使用require导入module.js:
var service = require('module.js') console.log(service.port) // 输出3000
ES6中使用export与import关键词实现模块化。
module.js中使用export导出port变量和getAccounts函数:
export var port = 3000 export function getAccounts(url) { ... }
main.js中使用import导入module.js,能够指定须要导入的变量:
import {port, getAccounts} from 'module' console.log(port) // 输出3000
也能够将所有变量导入:
import * as service from 'module' console.log(service.port) // 3000
Fundebug专一于JavaScript、微信小程序、微信小游戏、支付宝小程序、React Native、Node.js和Java实时BUG监控。 自从2016年双十一正式上线,Fundebug累计处理了6亿+错误事件,获得了Google、360、金山软件等众多知名用户的承认。欢迎免费试用!
转载时请注明做者Fundebug以及本文地址:
https://blog.fundebug.com/2017/08/21/10-best-es6-feature/