ES6

数组新特性

浅拷贝…

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 函数的语法糖
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值