数组新特性
浅拷贝…
var arr1 = [1,2,3,{name:'tom'}];
var arr2 = [...arr1] ; //这里属于浅拷贝
arr1[0] =4 ;
arr1[3].age = 18 ;
console.log(arr1); //[ 4, 2, 3, { name: 'tom', age: 18 } ]
console.log(arr2); //[ 1, 2, 3, { name: 'tom', age: 18 } ]
函数
箭头函数
- 基本形式
[1,2,3].map(x=>x+1);
generator函数
- ES6 提供的一种异步编程解决方案
执行特点
由于 Generator 函数返回的遍历器对象,只有调用next方法才会遍历下一个内部状态,所以其实提供了一种可以暂停执行的函数。yield表达式就是暂停标志。
遍历器对象的next方法的运行逻辑如下。
- 遇到yield表达式,就暂停执行后面的操作,并将紧跟在yield后面的那个表达式的值,作为返回的对象的value属性值。
- 下一次调用next方法时,再继续往下执行,直到遇到下一个yield表达式。
- 如果没有再遇到新的yield表达式,就一直运行到函数结束,直到return语句为止,并将return语句后面的表达式的值,作为返回的对象的value属性值。
- 如果该函数没有return语句,则返回的对象的value属性值为undefined。
通过next执行下一步
function* fm(){
yield
yield 1 + 2 ;
yield { name:'tom' } ;
};
var f = fm();
console.log( f.next() ); //{ value: undefined, done: false }
console.log( f.next() ); //{ value: 3, done: false }
console.log( f.next() ); //{ value: { name: 'tom' }, done: false }
console.log( f.next() ); //{ value: undefined, done: true }
调用next时传值
next里传入的值会作为上一个yield执行的结果
function* fm(){
var x = yield
yield 2 + x ;
};
var f = fm();
console.log( f.next() ); //{ value: undefined, done: false }
console.log( f.next(8) ); //{ value: 10, done: false }
console.log( f.next() ); //{ value: undefined, done: true }
return
function* fm(){
var x = yield
yield 2 + x ;
return 100 ;
};
var f = fm();
console.log( f.next() ); //{ value: undefined, done: false }
console.log( f.next(8) ); //{ value: 10, done: false }
console.log( f.next() ); //{ value: 100, done: true }
console.log( f.next() ); //{ value: undefined, done: true }
async函数
- 是 Generator 函数的语法糖