·  阅读 52

# 1var-let-const

``````//1.var声明前置变量
a =3
var a
var a = 4

//2.let不可声明前置变量
a =3
let a //报错

//3.let不可重复声明
let a =3
let a= 4  //报错
let a= 5  //报错

//4.存在块级作用域
for(leti =0; i<3；i++ ){
console.log(i)
}
console.log(i) //报错

//5.const 声明的常量不可改变
const a =1
a=2   //报错

const obj ={a:1}
obj.a =2  //没问题
obj{a:2}  //报错

# 2解构赋值

``````//1数组的解构
let [a,b,c] =[1,2,3]
console.log(a,b,c)
let {a,{b},c} =[2,[3],4]
a//2
b//3
c//4
let[a] =1 //报错

//2默认值
let[a,b=2] =[3,4]
a//3
b//4

let [a=2, b=3] =[undefined, null]
a//2
b//null

let[a=1,b=a] =[2]
a//2
b//2

``````//3对象的解构赋值
let [name, age] =['hunger',3]
let p1 ={name,age}
//等同于
let p2 ={name:name,age:age}

//解构范例

let {name,age} ={name:'ha', age:4}

//以上代码等同于
let name
let age
{{name:name,age:age}={name:'ha',age:4}}

``````//4默认值
let{x,y=5}={x:1}
x//1
y//5

``````//5函数解构
return x+y
}

function sum{{x,y}={x:0,y:0},{a=1,b=1}}{
return [x+a, y+b]
}
sum{{x:1,y:2},{a:2}} // [3 ,3]

``````// 6 作用
let[x,y] =[1,2]
[x,y]= [y,x]
x//2
y//1

# 3 字符串-函数-数组-对象

## 字符串

``````//1.多行字符串
let str = `
hi,
This is ha.com.
You are very good
`

//2.字符串模板
let website = 'ha'
let who ='you'
let str =`
this is \${website}.
\${who} are very good

`

## 数组

``````
var a =[1,2]
console.log(...a) //1,2
var b =[...a,3]
b//[1,2,3]

//函数参数的扩展
function sort(...arr){
console.log(arr.sort())

}
sort(3,1,5) //[1,3,5]

//类数组对象转数组
let ps =document.querySelectorAll('p');
Array.from(ps).forEach(p=>{
consoloe.log(p.innerText);
});
[...ps].forEach(p=>{console.log(p.innerText)});

## 函数

``````//1.默认值
function sayHi(name='ha'){
console.log(`hi,\${name}`)
}
sayHi()
sayHi('good')

# 模块化

``复制代码``