var let const 声明变量
var 可以重复声明,私有可能与全局冲突,并不提示错误; 函数级作用域.
let 块级,函数内重名的不会覆盖父级.
const 静态赋值,不能改变直接const直接申明的变量,但是可以更改声明的对象的属性值,如果强制限制不更改,使用如下,限制了更改.
const obj = {a:1,b:2} Object.freeze(obj);
箭头函数下的this
- 在箭头函数下,this指向不在上一层,只在父级的this
函数参数默认值
- 以前 a || 5; 现在,在参数中 function(a=5){..}
箭头函数不适用场景
作为构造函数,或一个方法需要绑定到原型对象 prototype
需要上级this的时候
需要使用arguments对象
字符串模板
关键符号 `` ,${..}
反引号可以嵌套,变量可以加入函数
标签模板字符串
在字符串前面加函数来定制
根据函数的返回,来指定返回的内容.替换掉不该有的字符串.
1
2
3
4
5const sentence = tmp`abc${ef}gh${ij}`
function tmp(string,...arg){
// string表示中间的字符串 abc,gh . 以变量开头的,会留下空的值
// arg 表示所有的变量 ef,ij
}
template string 中转译标签
- DOMPurify 一个验证插件
es6新增的字符串函数
.startsWith(str,start)
.endsWith(str,start)
.idcludes(str,strat)
.repeat(times)
对象解构
-
1
2
3
4
5
6
7
8const tom = {
a:1
b:2
c:3
}
// 结构,a重命名aa; 增加d,默认值123
const {a:aa,b,c,d = 123} = tom;
console.log(aa,b,c1);
数组解构
-
1
2
3
4
5const tom=[1,2,3,4,5,6];
const [a,b]=tom; // 1
const [a,b,,c] = tom; //2
const [a,...arg] = tom; // 3
const [a,b,c,d,e,f,e=7] = tom //4
for of
es下的循环
1
2
3
4
5
6
7
8
9
10comst doms = [1,2,3];
for(let i = 0; i < 5; i++){} // 1
doms.forEach(dom=>{...}) // 2
for(let i in doms){} // 3 可跳出 continue | break
for(let dom of doms){} // 4
for(let array of doms.entries()){
// down 是否完成
// value {key:值}
} // 5
for(let [i,dom] of doms.entries()){} // 6仅支持 arguments 对象.
Array .from() 和Array .of()
.from() 将不是数组原型的数组 nodelist|object 等,转换成数组原型.
Array.from(nodelist,list=>list.textContent);
数组原型可以使用的方法textcontent,reduce 等
.of() 保证数组的一致性.
Array.of(1,2) // 组成由传入的参数构成的数组
Array其他用法
.find(element,index,array) 返回对象.
.findIndex() 返回所在位置..索引
.same() 如果数组至少一个元素满足条件,就退出,并返回true
.every() 如果数组有一个没满足条件,就退出,并返回false;同意,只有全部满足条件才能返回true
剩余参数
- …args 将剩余参数放到一个数组里.
扩展运算符
concat //两个数组连在一起.
扩展到数组里.
1
2
3
4
5
6const a=[1,3,5,7]
const b=[2,4,6]
const r = [...a,...b] //1
const todos=[..document.querySelectorAll('li')]
对象字面量的扩展
导出同名属性,方法省略function,
1
2
3
4module.export={
a,b,c,
func(){...}
}计算属性 可在对象内使用函数
1
2
3
4
5
6
7
8const keys = [a,b,c]
const values = [1,2,3Ï]
const r = {
[keys.shift()]:values.shift(),
[keys.shift()]:values.shift(),
[keys.shift()]:values.shift(),
}
Promise
常用库 axios
-
1
2
3
4
5
6
7
8
9
10
11const p = new promise((resolve,reject)=>{
resolve('成功信息');
reject(Error('失败信息'));
})
p.then(data=>{
...
})
p.catch(err=>{
...
}) 链式调用
同步调用
1
2
3
4
5
6
7
8
9Promise
.all([obj1,obj2])
.then(
res=>{
const {o1,o2} = res;
console.log(o1,o2);
}
)
.catch(err=>{..})race 返回最先返回的结果.
Symbol
第7种数据类型 ,生成唯一的标识符
解决对象中key一样的问题.
不能遍历,统称用做私有属性.
获取对象
Object.getOwnPropertySymbols(obj).map(sbl=>{..})
构建模块
用nodejs构建
引入与导出
export default
import name from ‘..js’
// 只能有一个默认导出export const api = ‘abc’
export {api} from ‘..js’
// 命名导出可以多个.
原型继承和class
prototype
__proto__
class
extends 继承类
super 继承类,在构造函数中,传入父类的构造函数参数
1 | // 类 |
类的扩展
遍历器:Iterator
1 | let colors=[a,b,c] |
- next();
生成器:Generator
- *和yield
Proxy
set
不能重复
不能通过索引来获取值
去除数组重复.
const array = [2,3,5,4,3,2,5];
const numberSet = new Set(array);
const uniqueNumber = […numberSet];
1 | const colors = new Set(); |
weakSet
const peope = new WeakSet([obj1,obj2])
必须对象
不能for of | forEach循环
不能clear(); 自己clear
Map
1 | const people = new Map(); |
weakMap
const people = new weakMap();
键是对象
people.set(obj,1);
people.get(obj);
不用会清除
不能循环
不能size
不能has
不能delete
arguments
this.setState()
slug包
brower-sync
babel-cli
eslint:airbnb ; awesome-eslint