1.let、const块级作用域以及var的区别
- let、const声明的变量,会形成块级作用域,块级作用域内的变量,不能被作用域外部使用
- let、const声明变量不再会有声明提升,在变量声明前使用会报错
2.模板字符串
用法:使用··(反引号)将字符串包裹起来
功能:可以换行、使用标签函数进行字符串操作
//换行
let str = `fdsqak
fdsa`
console.log(str)
//带标签的模板字符串
let person ={
name:'xiaowang',
gender:1,}
//返回值为标签函数的返回值
let result = tagFunc`my name is ${person.name}.gender is ${person.gender}`
console.log(result);//my name is xiaowang.gender is 男
3.字符串扩展方法
- includes-是否包含
- startsWith-是否以什么开始
- endsWith-是否以什么结束
let str = 'abcd' console.log(str.includes('e'))//false console.log(str.startWith('a'))//true console.log(str.endWidth('a'))//false
4.展开数组
-
使用...将数组展开
let arr = [1,2,3] console.log(...arr) // 等价于es5中以下写法 console.log.apply(console, arr)
5.箭头函数
特性&优势:
-
1、简化了函数的写法
- 2、没有 this 机制,this 继承自上一个函数的上下文,如果上一层没有函数,则指向 window
3、作为异步回调函数时,可解决 this 指向问题
let inc = (n) => n + 1
console.log(inc(100))
let obj = {
name: 'aa',
func() {
setTimeout(() => {
console.log(this.name) //aa
}, 0)
setTimeout(function () {
console.log(this.name) //undefined
}, 0)
},
}
obj.func()
6.Set
说明:
Set 是一种类似于数组的数据结构
特性:
- 元素唯一性,不允许重复元素
使用add增加重复元素,将会被忽略
用途:
- 数组去重
- 数据存储
const arr = [1, 3, 1, 1, 1]
const set = new Set(arr)
set.add(1).add(1)
console.log(set.size) //2
const newArr = Array.from(set)
console.log(newArr) //[ 1, 3 ]
7.Map
说明:
类似 Object,以 key、value 形式存储数据
区别:
Map 键不会隐式转换成字符串,而是保持原有类型
let map = new Map()
map.set(1, 1)
map.set('name', 'map')
map.set(obj, obj)
console.log(map.get(1)) //1
/**
1 1
name map
{ '1': 1, true: true, a: 'a' } { '1': 1, true: true, a: 'a' }
*/
map.forEach((val, key) => {
console.log(key, val)
})