① 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()