码迷,mamicode.com
首页 > 其他好文 > 详细

我的es6

时间:2017-12-07 19:11:20      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:es6

1.ES6 ?

语法基础:

http://www.cnblogs.com/zhengjialux/p/6656962.html


问题:

1.

function add(...x){

? ? return x.reduce((m,n)=>m+n);

}

console.log(add(1,2,3));//输出:6

console.log(add(1,2,3,4,5));//输出:15



2.

const A = [1,2];

A.push = 3;

console.log(A); //[1,2,3]

A = 10; //Error



3.结构赋值

let [first,...last] = [1,2,3];

console.log(last); //[2,3]



4.

Rest + Spread


"..."


//Rest

function f(x, ...y) {

? return x * y.length;

}

f(3, "hello", true) == 6


//Spread

function f(x, y, z) {

? return x + y + z;

}

f(...[1,2,3]) == 6


5.

ES6中提供了用反引号`来创建字符串,里面可包含${…}等



6.

Generators


ES6中非常受关注的的一个功能,能够在函数中间暂停,一次或者多次,并且之后恢复执行,在它暂停的期间允许其他代码执行,并可以用其实现异步。


Run-Stop-Run...


function *foo(x) {

? ? var y = 2 * (yield (x + 1));

? ? var z = yield (y / 3);

? ? return (x + y + z);

}


var it = foo( 5 );


console.log( it.next() ); ? ? ? // { value:6, done:false }

console.log( it.next( 12 ) ); ? // { value:8, done:false }

console.log( it.next( 13 ) ); ? // { value:42, done:true }

generator能实现好多功能,如配合for...of使用,实现异步等等,我在这里就不多说了,详见。


我的es6

标签:es6

原文地址:http://blog.51cto.com/13496382/2048438

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!