- let const 存在块级作用域 不存在变量提升
1 .let
for(let i=0;i<3;i++){
let i='foo'
console.log(i) //输出三次foo
}
//等价于
let i =0
if(i<3){
let i='foo'
console.log(i)
}
i++
if(i<3){
let i='foo'
console.log(i)
}
i++
if(i<3){
let i='foo'
console.log(i)
}
i++
- const声明变量必须赋值 ,不能改变变量的引用地址
解构
//数组解构
const [,,m,,,n=1] = [1,2,3,5,7] //带默认值
const [a,b,...c] = [1,2,3,5,7] //...只能用在最后
console.log(m,n,a,b,c) // 3 1 1 2 [3,5,7]
//对象解构
//对象解构
const name = 'Tom'
const obj = {age:20}
const {name:newName="aaa"} = obj //名字冲突采用重命名
console.log(newName)
模板字符串
//模板字符串 支持换行 插值表达式
const x = 333
const str = `dff ${x} -- ${1+2} \`--
${Math.random()}`
console.log(str)
//带标签的模板字符串
const xm = 'Tom'
const age = 24
function myFun(str,name,age){
//函数内对参数进行加工处理
console.log(str) //静态的字符串组成的数组
console.log(name) //插人的动态值
console.log(age)//插人的动态值
return str[0] + name + str[1] + age
}
const str2 = myFun`hello ${xm} is a ${age}`
console.log(str2)
字符串扩展
const msg = 'Error: foo is not find'
console.log(msg.startsWith('Error'))
console.log(msg.endsWith('find'))
console.log(msg.includes('foo'))
参数默认直
//参数默认直
function foo(a,b=1){
console.log(a,b)
}
foo(1)
三点运算符
// ... 1.剩余参 2.展开
function foo(a,...rest){
console.log(a)
console.log(rest)
}
foo(1,2,3,4,5)
const arr = [1,2,3,4,5,6]
console.log(...arr)
箭头函数
//箭头函数 没有自己的this this取决于外部函数的this,没有则为window 不写大括弧默认返回表达式的值
const fn = (a,b) => a + b
console.log(fn(1,2))
对象字面量
//对象字面量
const a = 1
const obj = {
a, // => a:1
b(){
// => b:function(){}
},
[Math.random()]:1 //计算属性
}
console.log(obj)
对象扩展方法
//Object对象扩展方法
// Object.assign Object.assign
const source = {a:1,b:2}
const source2 = {d:5,e:7}
const target = {a:2,c:3}
const res = Object.assign(target,source,source2) //c
console.log(res) // {a:1,c:3,b:2}
console.log(res===target) //true
function func(obj){//保障函数内部对象不影响外部
const funcObj = Object.assign({},obj)
funcObj.name ='111'
console.log(funcObj)
}
const obj = {name:'Tom'}
func(obj)
console.log(obj)
// Object.is
console.log(Object.is(+0,-0))
console.log(Object.is(NaN,NaN))
proxy
//代理 proxy
const person = {
name:'Tom',
age:20
}
const personProxy = new Proxy(person,{
get(target,property){
return property in target?target[property]:''
},
deleteProperty(target,property){
delete target[property]
},
set(target,property,value){
console.log(target)
console.log(property)
console.log(value)
target[property] = value
}
})
console.log(personProxy.name)
personProxy.sex = 1
delete personProxy.sex
console.log(person)
//与object defineProperty区别
//1 defineProperty只能监视属性读写
//2 数组监视
const arr = []
const personProxy2 = new Proxy(arr,{
set(target,property,value){
console.log(target)
console.log(property)
console.log(value)
target[property] = value
return true
}
})
personProxy2.push(100)
class
//class
class Person{
static create(){
console.log('静态方法')
}
constructor(name){
this.name = name
}
say(){
console.log(this.name)
}
}
class Student extends Person{
constructor(name,number){
super(name) //调用父类构造函数 super指向父类
this.number = number
}
hello(){
super.say()
console.log(`my number is ${this.number}`)
}
}
const perosn = new Person('Tom').say()
Person.create()
new Student('Bob',20).hello()
Reflect
//Reflect 统一了对对象的操作方法
const obj = {
name:'Tom',
age:20
}
// console.log("name" in obj)
// console.log(delete obj.name)
// console.log(Object.keys(obj))
console.log(Reflect.has(obj,'name'))
console.log(Reflect.deleteProperty(obj,'name'))
console.log(Reflect.ownKeys(obj))
console.log(obj)
Set 集合 => 类似数组
//set
const s = new Set() //不重复的集合
s.add(1).add(2).add(2).add(3).add(4)
console.log(s.size) //集合长度
console.log(s.has(1)) //判断元素是否在集合中
console.log(s.delete(1)) //删除集合中的元素
console.log(s.clear()) //清空集合
for(let i of s){
console.log(i)
}
//数组去重
const arr = [1,2,2,2,2,3,3,5,6788,99]
const s2 = [...new Set(arr)]
console.log(s2)
Map =>对象
//Map 描述键值对的映射关系 可以用任意数据类型作为键,对象只能用字符串作为键
const m = new Map()
const tom = {name:'Tom',age:20}
m.set(tom,90)
m.forEach((value,key)=>{
console.log(value) //值
console.log(key) //键
})
console.log(m) // { { name: 'Tom', age: 20 } => 90 }
console.log(m.get(tom)) //获取键对应值
console.log(m.has(tom)) //是否有某个键
console.log(m.delete(tom)) //删除某个键
console.log(m.clear()) //清空键值
symbol
const name = Symbol('1') // Symbol每次创建都是唯一的。创建对象私有属性,外部无法获取对象对象
const obj = {
[name]:'Tom' //Symbol属性名无法通过for in Object.keys取到
}
console.log(Object.getOwnPropertySymbols(obj)) //获取symbol属性名
console.log(obj)
//复用symbol
const s1 =Symbol.for('1') //for里面是字符串,不是自动转字符串
const s2 =Symbol.for('1')
console.log(s1===s2) //true
//内置symbol常量
const obj2 = {
[Symbol.toStringTag]:'自定义toString'
}
console.log(obj2.toString()) //[object 自定义toString]
iterable接口
//iterable接口
const todos = {
life: ['吃饭', '睡觉', '打豆豆'],
learn: ['语文', '数学', '外语'],
work: ['喝茶'],
//迭代器
[Symbol.iterator]: function () {
const all = [...this.life, ...this.learn, ...this.work]
let index = 0
return {
next: function () {
return {
value: all[index],
done: index++ >= all.length
}
}
}
},
//generator方式
[Symbol.iterator]: function * () {
const all = [...this.life, ...this.learn, ...this.work]
for (let item of all) {
yield item //yield返回值 作为value {value:item,done:false}
}
}
}
for (let item of todos) {
console.log(item)
}
generator
//generator
function* foo() {
console.log('111')
yield 100
console.log('222')
yield 200
console.log('333')
yield 300
}
const res = foo()
console.log(res.next())
console.log(res.next())
console.log(res.next())
console.log(res.next())
// 111
// { value: 100, done: false }
// 222
// { value: 200, done: false }
// 333
// { value: 300, done: false }
// { value: undefined, done: true }
其他
//es2016
const arr = [1, 2, NaN]
console.log(arr.indexOf(NaN)) //不能查找NaN -1
console.log(arr.includes(NaN)) //true
console.log(2**10) //2的十次方
//es2017
let obj = {
name:'Tom',
age:20
}
console.log(Object.values(obj)) //[ 'Tom', 20 ]
console.log(Object.entries(obj)) //[ [ 'name', 'Tom' ], [ 'age', 20 ] ]
//将对象转map
console.log(new Map(Object.entries(obj)))
console.log(Object.values(obj))
const p1 = {
firstName:'Li',
lastName:'wei',
get fullName(){
return this.firstName + ' ' + this.lastName
}
}
const p2 = Object.assign({},p1)
p2.firstName = 'H'
console.log(p2) //get set描述信息不会更新 Li wei
const des = Object.getOwnPropertyDescriptors(p1)
const p3 = Object.defineProperties({},des)
p3.firstName = 'M'
console.log(p3.fullName) //M wei
const str = 'dfs'
console.log(str.padEnd(10,'-')) //dfs-------
console.log(str.padStart(5,'0')) //00dfs