1ES6速学

·  阅读 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函数解构
functon add([x=1,y=2]){
return x+y 
}
add() //3
add([2]) //4
add([3,4]) //7

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')

复制代码

模块化

复制代码
分类:
前端
标签:
分类:
前端
标签:
收藏成功!
已添加到「」, 点击更改