es6引入rest参数,用于获取函数的实参,用来代替arguments
rest参数必须在形参中写明,方式:三个点加名称func(a,b,c,...other)与arguments的区别:arguments无需显式声明;args是伪数组(本质是对象),rest是数组,可使用数组的所有api作用:将数组转换为逗号分隔的参数序列
生成器是es6提供的一种异步编程解决方案,本质是一个特殊的函数 声明方式
function * gen(){ } //生成器的声明方式与普通函数的区别在于function和函数名之间多了一个星号执行方式
function* gen() { console.log(111); yield '第一次'; //yield后可跟表达式或字面量 console.log(222); yield '第二次'; console.log(333); yield '第三' + '次'; console.log(444); } let gen1 = gen(); gen1.next(); gen1.next(); gen1.next(); gen1.next();实例代码
//基本用法 <script> let p = new Promise(function(resolve, reject) { setTimeout(() => { let data = "seccessfully." // resolve(data); let err = "failed" reject(err); }, 1000); }) p.then(function(value) { console.log(value); }, function(reason) { console.log(reason); }) </script> //promise封装ajax请求 <script> const p = new Promise(function(resolve, reject) { const address = 'https://api.a888piopen.top/getJoke' const xhr = new XMLHttpRequest(); xhr.open("GET", address); xhr.send(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status >= 200 && xhr.status <= 300) { resolve(xhr.response) } else { reject(xhr.status) } } } }) p.then(function(value) { console.log(value); }, function(reason) { // console.error(reason); console.log("cuole!") }) </script> //promise封装读取文件 const fs = require('fs'); let p = new Promise(function(resolve, reject) { fs.readFile('./some.md', (err, data) => { if (err) reject(err); resolve(data) }) }) p.then(function(value) { console.log(value.toString()); }, function(reason) { console.log(reason); })then()方法的返回结果也是一个promise对象,对象的状态(promise的promiseState属性值)由回调函数的执行结果决定:如果回调函数中返回的结果是 非promise类型的数据类型,对象状态为成功,返回值(promise的promiseResult属性值)即为成功函数的返回值;如果回调函数中返回的结果是promise对象实例,则该promise对象实例的返回结果即为外层promise对象的返回值