ES6新特性
1、箭头函数
eg:
(a, b) => a * b
等价于
function (a, b) { return a * b }
具体箭头函数讲解, 请看:http://www.javascriptkit.com/javatutors/javascriptarrowfunctions.shtml
2、Promises
const mypromise = new Promise(function(resolve, reject){
// 在这编写异步代码
// 调用 resolve() 来表示任务成功完成
// 调用 reject() 来表示任务失败
})
function getasync(url) { return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest() xhr.open("GET", url) xhr.onload = () => resolve(xhr.responseText) xhr.onerror = () => reject(xhr.statusText) xhr.send() }) } getasync('test.txt').then((msg) => { console.log(msg) // echos contents of text.txt return getasync('test2.txt') }).then((msg) => { console.log(msg) // echos contents of text2.txt return getasync('test3.txt') }).then((msg) => { console.log(msg) // echos contents of text3.txt })
具体Promise请看: http://www.javascriptkit.com/javatutors/javascriptpromises.shtml
3、异步函数
a、一个以 async
为前缀的常规函数
async function fetchdata(url){ // Do something // Always returns a promise }
b、在异步函数(Async function)内,使用 await
关键字调用异步操作函数
function getasync(url) { // same as original function return new Promise((resolve, reject) => { const xhr = new XMLHttpRequest() xhr.open("GET", url) xhr.onload = () => resolve(xhr.responseText) xhr.onerror = () => reject(xhr.statusText) xhr.send() }) } async function fetchdata(){ // main Async function var text1 = await getasync('test.txt') console.log(text1) var text2 = await getasync('test2.txt') console.log(text2) var text3 = await getasync('test3.txt') console.log(text3) return "Finished" } fetchdata().then((msg) =>{ console.log(msg) // logs "finished" })
4、解构
var profile = { name:'George', age:39, hobby:'Tennis' } var {name, hobby} = profile // destructure profile object console.log(name) // "George" console.log(hobby) // "Tennis"
使用别名,你可以使用与你正在提取值的对象属性不同的变量名:
var profile = {name:'George', age:39, hobby:'Tennis'} var {name:n, hobby:h} = profile // destructure profile object console.log(n) // "George" console.log(h) // "Tennis"
解构也可以与嵌套对象一起工作,我一直使用它来快速解开来自复杂的JSON请求的值:
var jsondata = { title: 'Top 5 JavaScript ES6 Features', Details: { date: { created: '2017/09/19', modified: '2017/09/20', }, Category: 'JavaScript', }, url: '/top-5-es6-features/' }; var {title, Details: {date: {created, modified}}} = jsondata console.log(title) // 'Top 5 JavaScript ES6 Features' console.log(created) // '2017/09/19' console.log(modified) // '2017/09/20'
数组的解构与在对象上的工作方式类似,除了左边的花括号使用方括号代替:
var soccerteam = ['George', 'Dennis', 'Sandy'] var [a, b] = soccerteam // destructure soccerteam array console.log(a) // "George" console.log(b) // "Dennis"
或者
var soccerteam = ['George', 'Dennis', 'Sandy'] var [a,,b] = soccerteam // destructure soccerteam array console.log(a) // "George" console.log(b) // "Sandy"
解构详细请看:https://hackernoon.com/getting-to-grips-with-es6-destructuring-e5b5ddb34990
5、默认和剩余参数
a、默认参数(Default Parameters)
function getarea(w,h){ var w = w || 10 var h = h || 15 return w * h }
b、剩余参数(Rest Parameters)
function addit(...theNumbers){ // get the sum of the array elements return theNumbers.reduce((prevnum, curnum) => prevnum + curnum, 0) } addit(1,2,3,4) // returns 10
通过在命名参数前添加3个点 ...
,在该位置和之后输入到函数中的参数将自动转换为数组。
function addit(theNumbers){ // force arguments object into array var numArray = Array.prototype.slice.call(arguments) return numArray.reduce((prevnum, curnum) => prevnum + curnum, 0) } addit(1,2,3,4) // returns 10
Rest parameters 只能应用于函数的参数的一个子集,就像下面这样,它只会将参数从第二个开始转换为数组:
function f1(date, ...lucknumbers){ return 'The Lucky Numbers for ' + date + ' are: ' + lucknumbers.join(', ') } alert( f1('2017/09/29', 3, 32, 43, 52) ) // alerts "The Lucky Numbers for 2017/09/29 are 3,32,43,52"
本文章借鉴于http://www.webhek.com/post/top-five-features-in-javascript-es6-worth-mastering.html