es java script_javescript经验文档(es6/es2017篇)

迭代器 - Iterators

{

'use strict'

function chef(foods){

let i = 0;

return {

next(){

let done = (i >= foods.length);

let value = !done ? foods[i++]:undefined;

return{

value:value,

done:done

}

}

}

}

let wanghao = chef(['西红柿','孙双双']);

console.log(wanghao.next());

console.log(wanghao.next());

console.log(wanghao.next());

}

结果:

{value: "西红柿", done: false}

{value: "孙双双", done: false}

{value: undefined, done: true}

生成器 - Generators

{

'use strict'

function* chef(){

yield '西红柿';

yield '炒蛋';

}

let wanghao = chef();

console.log(wanghao.next());

console.log(wanghao.next());

console.log(wanghao.next());

}

结果:

{value: "西红柿", done: false}

{value: "孙双双", done: false}

{value: undefined, done: true}

模版字符串 - ``

{

// 普通字符串

let str = `In JavaScript '\n' is a line-feed.`;

console.log(str);

// 多行字符串

let str2 = `In JavaScript this is

not legal.`;

console.log(str2);

// 字符串中嵌入变量

let name = 'liushi';

window.location.href = `http://www.baidu.com?name=${name}`;

}

函数的参数默认值

// ES6之前,当未传入参数时,text = 'default';

function printText(text) {

text = text || 'default';

console.log(text);

}

// ES6;

function printText(text = 'default') {

console.log(text);

}

Spread / Rest 操作符

当被用于迭代器中时,它是一个 Spread 操作符:

function foo(x,y,z) {

console.log(x,y,z);

}

let arr = [1,2,3];

foo(...arr); // 1 2 3

当被用于函数传参时,是一个 Rest 操作符:当被用于函数传参时,是一个 Rest 操作符:

function foo(...args) {

console.log(args);

}

foo( 1, 2, 3, 4, 5); // [1, 2, 3, 4, 5]

for...of 和 for...in

{

//返回值

let letters = ['a', 'b', 'c'];

letters.size = 3;

for (let letter of letters) {

console.log(letter);

}

// 结果: a, b, c

//返回键,es6

let stus = ['Sam', '22', '男'];

stus.size = 3;

for (let stu in stus) {

console.log(stu);

}

// 结果: 0,1,2,size

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值