ES6

let变量
let a, b, c;
let strs = 'new';
{ let name = '张三'; var school = 'keshi'; } console.log(school); console.log(name)
console.log(song)
|
const常量
const SCHOOL = '尚硅谷';
{ const NAMES = "hello"; }
const TEAM = ['uzi', 'yly']; TEAM.push('ne'); console.log(TEAM);
|
变量的赋值
const F4 = ['小沈阳', '刘能', '赵四', '宋小宝']; let [xiao, liu, zhao, song] = F4; console.log(xiao); console.log(liu); console.log(zhao); console.log(song);
const bao = { name: '宋小宝', age: '不详', xiaopin: function() { console.log('我能演小品'); } }
let { xiaopin } = bao; xiaopin();
|
模板字符转
let strs = `看我 换行`; console.log(strs);
let lovest = '苏炳添'; let out = `${lovest}是短跑冠军`; console.log(out);
|
简化对象写法
let name = '尚硅谷'; let chang = () => { console.log('我可以改变你'); }
const SCHOOL = { name, chang, defau() { console.log('我可以提高你的技能'); } } console.log(SCHOOL);
|
箭头函数
function getName() { console.log(this.name); } let getName2 = () => { console.log(this.name); } const SCHOOL = { name: "VUE" } window.name = "尚硅谷";
getName(); getName2();
getName.call(SCHOOL); getName2.call(SCHOOL);
let Person = function(name, age) { this.name = name; this.age = age; } let me = new Person("xiao", 20); console.log(me);
let add = n => { return n + n; } console.log(add(2));
let pow = n => n * n; console.log(pow(9)); let pows = (n, m) => n * m; console.log(pows(38, 29))
|
函数默认值
function add(a, b, c = 10) { return a + b + c; } let result = add(1, 2); console.log(result);
function connsct({ host = 183, username, password, port }) { console.log(host); console.log(username); console.log(password); console.log(port); }
connsct({ username: "root", password: "root", port: 13306 })
|
rest 参数
function date(...ages) { console.log(ages); } date(2, 4, 7, 5, 3);
function fn(a, b, ...ages) { console.log(a, b, ages); } fn(1, 2, 3, 4, 5, 6, 7, 8)
|
扩展运算符
const ARR = ['易烊千玺', '王源', '王俊凯'];
function chunwan() { console.log(arguments); } chunwan(...ARR);
|
const kuaizi = ['王太利', '肖央']; const fenghuang = ['曾毅', '玲花'];
const zuixuan = [...kuaizi, ...fenghuang]; console.log(zuixuan);
const divs = document.querySelectorAll('div'); const divArr = [...divs]; console.log(divArr);
|
Map对象
ES6 提供了 Map 数据结构. 它类似于对象, 也是键值对的集合.但是”健”的范围不限于字符串. 各种类型的值(包括对象都可以当作健.Map 也实现了 iterator 接口, 所以可以使用[扩展月算符] 和 [for…of] 进行遍历. Map的属性和方法;
name |
function |
size |
返回Map的元素个数 |
set |
增加一个新的元素 |
get |
返回键名对象的键值 |
has |
检测Map中是否包含某个元素, 返回 boolean值 |
clear |
清空集合, 返回undefined |
Map数组的使用
let m = new Map(); m.set('names', '尚硅谷'); m.set('chang', () => { console.log("我可以改变你") }); let key = { school: 'ATGUIGU' }; m.set(key, ['上海', '北京', '深圳']);
for (let v of m) { console.log(v); }
|
class类
ES6提供了更接近传统语言的写法, 引入了 class (类) 这个概念,作为对象的模板. 通过class关键字, 可以定义类.基本上, ES6 的class 可以看作只是一个语法糖, 它的绝大部分功能, ES5 都可以做到, 新的class 写法只是让对象原型更清晰. 更像面向对象编程语法而已.
知识点:
class |
声明类 |
constructor |
定义构造函数初始化 |
extends |
继承父类 |
super |
调用父级构造方法 |
static |
定义静态方法和属性 |
|
父类方法可以重写 |
class使用
{ function Phone(brand, price) { this.brand = brand; this.price = price; }
Phone.prototype.call = function() { console.log('我可以打电话'); }
let Huawei = new Phone('华为', 5999); Huawei.call(); console.log(Huawei); }
class Phone { constructor(brand, price) { this.brand = brand; this.price = price; }
call() { console.log('我可以打电话') } }
let onePlus = new Phone('小米', 2999); onePlus.call(); console.log(onePlus);
|
static
class Phone { static name = '手机'; static change() { console.log('我可以改变世界'); } } let nokia = new Phone(); console.log(nokia.name); console.log(Phone.name); Phone.change();
|
使用ES5构造函数来实现继承
function Phone(brand, price) { this.brand = brand; this.price = price; }
Phone.prototype.call = function() { console.log('我可以打电话'); }
function SmartPhone(brand, price, color, size) { Phone.call(this, brand, price); this.color = color; this.size = size; }
SmartPhone.prototype = new Phone;
SmartPhone.prototype.photo = function() { console.log('我可以拍照'); }
SmartPhone.prototype.playGame = function() { console.log("我可以玩游戏"); }
const chuizi = new SmartPhone('锤子', 2499, 'black', '5.5inch');
console.log(chuizi); chuizi.playGame(); chuizi.photo();
|
使用ES6构造函数来实现继承
class Phone { constructor(brand, price) { this.brand = brand; this.price = price; }; call() { console.log("我可以打电话"); } } let p = new Phone('mi', 2999); console.log(p); p.call(); console.log('-------子类---------'); class SmartPhone extends Phone { constructor(brand, price, color, size) { super(brand, price); this.color = color; this.size = size; } playGame() { console.log('我可以玩游戏'); } photo() { console.log('我可以拍照'); } }
const chuzi = new SmartPhone('锤子', 9999, 'black', '5.5inch'); console.log(chuzi); chuzi.call(); chuzi.playGame(); chuzi.photo();
|
class 中 get 和 set
class Phone { get price() { console.log('价格被读取了'); return 'iloveyou'; }
set price(newval) { console.log('价格被修改了'); console.log(newval); } }
let s = new Phone(); console.log(s.price); s.price = 1;
|
API
Set数组
let s = new Set(); let s2 = new Set(['大事儿', '小事儿', '好事儿', '坏事儿', '小事儿']);
for (let v of s2) { console.log(v); }
|
Set使用
let arr = [1, 2, 3, 3, 5, 3, 7, 6, 3, 8, 3]; let arr2 = [3, 4, 5, 6, 7, 5];
let diff = [...new Set(arr)].filter(item => !(new Set(arr2).has(item))); console.log(diff);
|
Symbol数据类型
Symbol 是类似于字符转的 数据类型
- Symbol的值是唯一的, 用来解决命名冲突问题
- Symblo 值不能与其他数据类型进行运算
- Symbol 定义的对象属性不能只用 fot..in 循环遍历, 但是可以使用Reflect.ownKeys来获取对象的所有健名
let s = Symbol("hello"); console.log(s, typeof s); let s1 = Symbol('尚硅谷'); let s2 = Symbol('尚硅谷'); console.log(s1 === s2); let s3 = Symbol.for("张三"); let s4 = Symbol.for("张三"); console.log(s3 === s4);
|
Symbol 的使用
let game = {}; let methods = { up: Symbol(), down: Symbol() }; game[methods.up] = function() { console.log('我是up'); } game[methods.down] = function() { console.log('我是down'); } console.log(game);
|
Symbol用来做类型检测
class Person { static[Symbol.hasInstance](param) { console.log(param); console.log('我被用来检测类型了'); } }; let o = {}; console.log(o instanceof Person)
|
Symbol数据展开
const arr = [1, 2, 3]; const arr2 = [4, 5, 6]; arr2[Symbol.isConcatSpreadable] = false; console.log(arr.concat(arr2));
|
迭代器
迭代器是一种接口, 为各种不同的数据结构提供统一的访问机制.任何数据结构只要部署 Iterator 接口, 就可以完成遍历操作.
- ES6 创造了一种新遍历命令 for…of 命令, Iterator 接口主要提供for..of消费
- 原生具备 Iterator 接口的数据(可用 for .. of 遍历)
const arr = ['唐僧', '猪八戒', '孙悟空', '沙僧'];
let iterator = arr[Symbol.iterator](); console.log(iterator); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next());
|
自定义遍历
const Ban = { name: '终极一班', stus: [ '小明', '小红', '小宁' ], [Symbol.iterator]() { let index = 0; return { next: () => { if (index < this.stus.length) { const result = { value: this.stus[index], done: false } index++; return result; } else { return { value: undefined, done: true } } } }; } } for (let v of Ban) { console.log(v); }
|
生成器
function* fen() { console.log(111); yield 'one'; console.log(222) yield 'two'; console.log(333); yield 'three'; console.log(444); } let iterator = fen(); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next()); console.log(iterator.next()); console.log('-------------') for (let v of fen()) { console.log(v); }
|
回调函数
setTimeout(() => { console.log(111); setTimeout(() => { console.log(222); setTimeout(() => { console.log(333); }, 3000); }, 2000); }, 1000)
function one() { setTimeout(() => { console.log(111); iterator.next(); }, 1000) };
function two() { setTimeout(() => { console.log(222) iterator.next(); }, 1000) };
function three() { setTimeout(() => { console.log(333); iterator.next(); }, 1000) }
function* gen() { yield one(); yield two(); yield three(); }
const iterator = gen(); iterator.next();
|
Promise
const p = new Promise(function(resolve, reject) { setTimeout(function() {
let err = '数据读取失败'; reject(err); }, 1000) }); p.then(function(value) { console.log(value); }, function(reason) { console.log(reason); });
|
const p = new Promise((resolve, reject) => { setTimeout(() => { reject("出错了"); }, 1000) });
p.then(value => { console.log(value); }, reason => { console.warn(reason); })
|
处理文件的使用
const { rejects } = require('assert'); const fs = require('fs');
const p = new Promise(function(resolve, reject) { fs.readFile('./文件/xue.md', (err, data) => { if (err) reject(err); resolve(data); }); });
p.then(function(value) { console.log(value.toString()); }, function(reason) { console.log("读取失败!!!"); })
|