ES6重要语法

① map遍历

1、创建map实例

var map = new Map([['one',1], ['two', 2], ['three', 3]]);

2、map遍历

var users = [
    {name: "zhang", "email": "zhang@email.com"},
    {name: "jiang",   "email": "jiang@email.com"},
    {name: "li",  "email": "li@email.com"}
];
var emails = users.map(function (user) {
    return user.email
})

② 箭头函数

var obj = {
    birth: 1992,
    getAge: function () {
        var fn = function() {
            return this.birth
        };
        return fn();
    }
};
console.log(obj.getAge()) //undefined
 
var obj = {
    birth: 1992,
    getAge: function () {
        var fn = ()=> {
            return this.birth
        };
        return fn();
    }
};
console.log(obj.getAge()) //1992
现在,箭头函数完全修复了this的指向,this总是指向词法作用域,也就是外层调用者obj
var arr = [2,1,4,3,1];
arr.sort((x,y)=>{
    return x<y
})

③ promise

是异步编程的一种解决方案

var p = new Promise((resolve,reject)=>{
    var a = 1;
    resolve(a)
}).then(function (data) {
        return ++data;
    }
).then(function (data) {
        console.log(data)
    }
)

    ④ class

    通过`class`关键字,可以定义类。让对象原型的写法更加清晰、更像面向对象编程的语法而已。

    

class father{
    constructor(width,height) {
        this.width = width;
        this.height = height;
    }
    get step() {
        return this.number();
    }
    number() {
        return this.width * this.height;
    }
}
const son = new father(2,3);
Class 可以通过`extends`关键字实现继承
father.prototype.ext = function () {
    console.log(this.width);  //4
}
class father1 extends father{
    ext() {
        super.ext()
    }
}
const son1 = new father1(4,5);
son1.ext()
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值