一. const定义常量
ES6之前并没有定义声明常量的方式,ES6标准中引入了新的关键字const来定义常量。
const a = 1;
a = 3;
console.log(a);//报错
二、let块级变量
用let定义的变量只在块当中起作用,离开变量外界的块(括号)就会被销毁。
if(2 >1){
let a =10;
}
console.log(a);// 报错 a is not undefine
三.模板字面量
用于字符串拼接和写模板,使用`(反引号,左上角波浪线),变量使用${}.
可以写dom结点(html标签)
var age = 10,
name = "xiaoming",
print = `${name} is ${age}`
console.log(print);
四、解构赋值
1.交换值
// 解构赋值
var a = 1,
b = 2;
console.log(a,b);
[a, b] = [b, a];
console.log(a,b);
2.从数组中的某些项获取值
var a = [1,2,3,4]
var x,y;
console.log(x,y);
[x,,,y] = a;
console.log(x,y);
3.从对象中获取值,注意值的名字要和交换对象的属性一致
var obj = {
name :"abc",
age :18,
sex: "1",
functions:function test(){
return "函数"
}
}
var {age,functions} = obj;
console.log(age,functions)
五.箭头函数
methos = ()=>{
console.log("箭头函数");
}
methos();
//forEach:遍历数组每一项
var a = [1,2,3];
a.forEach((item,index)=>{
console.log(item);
})
//map方法:遍历数组每一项
var a = [1,2,3];
// a.forEach((item,index)=>{
// console.log(item);
// })
var new_a = a.map((item,index)=>{
return item+`${index+1}`;
})
console.log(new_a);
六.对象中函数的声明
var obj = {
name :"abc",
fun(){
console.log("函数定义")
}
};
console.log(obj.fun);
七.for-of循环遍历
var arr=[ "aaaa" , "bbbb" , "cccc" , "ddddd"" ];
for(var i of obj)[
console.log(i)
}
八.函数默认参数
function fun(x =10 ,y = 20, z=30){
console.log(x,y,z)
}
fun(1,2)
fun();
九展开运算符…
var a = ["a","b","c","d"],
b = [1,2,3,4],
c = ['A','B','C','D'];
//老连接数组的方式
var arr = a.concat(b,c);
console.log(arr);
//展开运算符
var arr1 = [...a,...b,...c];
console.log(arr1);