【原创】ES6 新特性

let

  • 声明的变量不能重复
  • 块级作用域
  • 不存在变量提升(在变量声明之前去使用)

const

值不能修改的量称为常量。

  • 一定要赋初始值
  • 一般常量使用大写
  • 常量的值不能修改
  • 块级作用域
  • 对于数组和对象的修改,不算对常量的修改,不会报错

变量解构赋值

// 数组解构
const F4 = ['小沈阳','刘能','赵四','宋小宝'];
let [x,l,z,s] = F4;
// 对象解构
const obj = {
name:"赵本山",
age:60,
actor:function() {
    console.log('小品');
}
}

let {name,age,actor} = obj;
let {actor} = obj;
actor();

模板字符串

  • 内容中可以直接出现换行符
  • 内容中可以出现变量
let str = `<ul>
<li>沈腾</li>
<li>马丽</li>
<li>艾伦</li>
</ul>`;

let name = '赵本山';
let tem = `${name}是我心中最幽默的人`;

对象的简化写法

    const zhao = {
        name:"zhao",
        age:69,
        xiaopin() {
            console.log('xiaopin');
        }
    };
    let {name,xiaopin} = zhao;
    xiaopin();
    console.log(name);

箭头函数

  • 箭头函数的 this 是静态的,this 始终指向函数声明时所在作用域下的 this 值。
  • 箭头函数不能作为构造函数去实例化对象
  • 不能使用 arguments 变量
  • 箭头函数可以有简化形式,当只有一个参数时,小括号可以省略,当函数体只有一个语句时,可省略花括号。
let pow = n => n * n;
  • 箭头函数适合与 this 调用无关的回调
  • 箭头函数不适合与 this 有关的回调,事件回调、对象的方法

可以给函数参数设置默认值

rest 参数

function fn(a, b, ...args) {
    console.log(a);
    console.log(b);
    console.log(args);
}

fn(1, 2, 3, 4, 5, 6);

扩展运算符

const tfboys = ['易烊千玺', '王源', '王俊凯'];
let arr = ...tfboys;

Symbol

数据类型

USONB // you are so niubillity

// u  undefined
// s  string symbol
// o object
// n null number
// boolean

iterator 迭代器

数组、map、对象、NodeList、Set、String、TypeArray、Arguments 都有迭代器。

const actors = ['沙溢','闫妮','姚晨'];

for (let v of actors) {
    console.log(v);
}

实际应用场景:

function getUsers() {
    setTimeout(() => {
    let data = '用户数据';
    iterator.next(data);
    }, 1000)
}
function getOrders() {
    setTimeout(() => {
    let data = '订单数据';
    iterator.next(data);
    }, 1000)
}
function getGoods() {
    setTimeout(() => {
    let data = '商品数据';
    iterator.next(data);
    }, 1000)
}
function * gen() {
    let users = yield getUsers();
    console.log(users);
    let orders = yield getOrders();
    console.log(orders);
    let goods = yield getGoods();
    console.log(goods);
}

let iterator = gen();
iterator.next();
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注