ES6新增语法
let 和 const
let
let a = 19;
let a ="张三";
console.log(a);
console.log(name);
let name = "张三";
if(1)
{
let a = 20;
}
console.log(a);
var a = 10;
{
console.log(a);
let a = 20;
}
const
1.const用来定义常量,一般常量要大写 2.常量一旦被定义就不能被修改了 3.使用const声明常量的同时还要赋初始值 const PI = 3.1415926 否则会报错
const PI = 3.1415926;
PI = 3.14;
console.log(PI);
附加: const 其实const有时候也是可以被修改的 对于一些引用数据类型像数组或者对象还是会被修改的
const arr = [12,34,56,67,90];
arr[2] = 30;
console.log(arr);
解构赋值
数组解构
let [a,b,c] = [12,34,56];
console.log(a,b,c);
let [a,b] = [12,34,56];
console.log(a,b);
let [a,b,c] = [12,34];
console.log(a,b,c);
let [a=0,b=5,c=10] = [10,20];
console.log(a,b,c);
let [,b] = [20,30];
console.log(b)
对象解构
let {name,age} = {"name":"张三","age":20};
console.log(name,age);
let {name:n,age:a} = {"name":"李四","age":24};
console.log(n,a);
let {age,name} = {"name":"李四","age":24};
console.log(age,name);
let {name,...a} = {"name":"张三","age":23,"gender":"男"};
console.log(name,a);
let obj = {
"name":"张三",
"age":23,
"hobby":{
"listfirst":"唱歌",
"listsecond":"跳舞"
}
}
let {hobby:{listfirst,listsecond}} = obj;
console.log(listfirst,listsecond);
reset 和 spread
reset 用来将参数存放到一个数组中
function number(...a)
{
return a;
}
let arr = number(10,23,34,50);
console.log(arr);
function number(...a,20)
{
return a;
}
let arr = number(10,23,34,50);
console.log(arr);
function number(b,...a)
{
return a;
}
let arr = number(10,23,34,50);
console.log(arr);
spread
let arr = [12,34,56,67];
console.log(...arr);
let arr = [12,34,56,67];
let arr2 = [...arr];
console.log(arr2);
let arr = [12,34,56,67,[20,40]];
let arr2 = [...arr];
console.log(arr2);
arr2[4][1] = 15;
console.log(arr);
let obj = {
"name":"张三",
"age":24,
"hobby":"打球"
}
let obj2 = {...obj};
console.log(obj2);
let arr1 = [12,24,56];
let arr2 = [67,89,80];
let arr3 = [...arr1,...arr2];
console.log(arr3);
箭头函数()=>{}
箭头函数特点: 减少函数代码量,回调函数用的频率比较高
function fuc(a) {
return a
}
console.log(fuc(20));
let c = (b)=>{
return b;
}
console.log(c(10));
let func = (a,b,c)=>{
return a+b+c;
}
console.log(func(10,20,30));
let func = a =>{return a;};
console.log(func(30));
let func = (...arg)=>{return arg}
console.log(func(10,20,50,60));
let func = (a=10)=>{return a};
console.log(func());
let func = (a,b)=> a + b;
console.log(func(10,20));
let func = (a)=>{console.log(a);}
let fc = new func(10);
console.log(fc);
let func = ()=>{
console.log(this);
}
func();
function func2()
{
return {
"func3":()=>{
console.log(this);
}
}
}
let obj = func2();
obj.func3();
function func4(i)
{
if(i==1)
{
return {
name:"张三",
skill:function()
{
return ()=>
{
console.log(this);
}
}
}
}
}
let obj = func4(1);
let b = obj.skill();
b();
iterator迭代器
介绍: 我们知道数组迭代可以使用every,some,filter,map,foreach,但是有类似数组结构也想使用这样的方法是不可以的,所以es6提出的iterator迭代器供多种数据结构使用,array,string,dom元素集合,等等伪数组,但是数组不可以使用。 介绍2:iterator是一种接口机制,为不同数据结构提供统一的访问,为不同的数据机构提供不同接口
let str = "1234567890";
let arr = [];
for(let key of str)
{
arr.push(Number(key)+1);
}
console.log(arr);
let Oli = document.getElementsByTagName("li"];
let arr = [];
for(let key of Oli)
{
arr.push(key);
}console.log(arr);
find和findIndex()
let arr = [12,34,56,78,20];
let result = arr.find((value)=>{
return value > 30;
});
console.log(result);
let result2 = arr.findIndex((value)=>{
return value > 30;
});
console.log(result2);
includes()和startsWith()、endsWith()
let arr = [1,2,3,4,5,6];
console.log(arr.includes(2));
console.log(arr.includes(7));
console.log(arr.includes(3,4));
let str = "123.jpg";
console.log(str.startsWith("123",0));
console.log(str.endsWith("jpg"));
set和map
特点:类似数组,成员值唯一(可以对数组进行去重) 语法:let set = new Set(数组||伪数组) 常见方法: size: 获取集合的长度 add(值) 1.在set集合中添加成员返回一个新的集合 特点:会修改原来的集合 2.向集合中添加两个NaN,结果集合中只出现了一个,原因是es6中NaN == NaN; delete(值) 在set集合中删除成员,返回是否删除成功 has(值):判断集合中是否有这个元素存在 clear()清空集合
let set = new Set([12,20,30,35]);
set.add(10);
console.log(set);
console.log(set.size)
console.log(set.delete(12))
console.log(set)
console.log(set.has(30));
let arr = [12,34,56,12,12];
let arr2 = new Set(arr);
console.log(arr2);
|