标签:new fun values argument 运算符 value style ret 对象
es6 常用的对象
集合:set , map
let set=new Set(); set add(1); set add(‘‘); set add(true); set add({age:20}); console.log(set); console.log(set.size); // 4 for(let value of set){ console.log(value); }
访问,只能用循环
set去重 let distinAry=function(ary){ let set=new Set(ary); return [...set]; //...扩展 console.log(distinAry([1,3,5,7,4,3,2,3])); }
let ary=[1,2,3]; let ary1=[4,5,6,7]; console.log([...ary,...ary1]); // [1,2,3,4,5,6,7] let obj={a:1,b:2,c:3}; //let obj1={...obj}; //console.log(obj1); let obj1={...obj,d:4}; console.log(obj1); // {a:1,b:2,c:3,d:4}
arguments不能扩展
console.log(...arguments); // undefined
存放元素为键值对
let map=new Map(); map.set(‘name‘,‘张‘); map.set(‘age‘,22); map.set(‘sex‘,‘男‘); console.log(map); console.log(map.get(‘name‘)); // 张 // 循环:for of for(let v of map){ console.log(v); console.log(v[0]); console.log(v[0],v[1]); }
方法
size :长度 console.log(map.get(‘name‘),map.size);
has:返回true/false console.log(map.has(‘name‘));
delete:删除 map.delete(‘sex‘);
clear:清空 map.clear();
keys:键 console.log(map.key());
values:得到所有值
map跟二维数组相对应 [...map]——二维数组
es6 ,让代码更易理解
let ary=[22,‘ww‘,‘女‘,1333333]; let age=ary[0]; let name=ary[1]; let sex=ary[2]; let phone=ary[3]; let[age,name,sex,phone]=ary; console.log(age,name,sex,phone); 或: let obj={a:1,b:2,c:3}; //let {a,b,c}=obj; //console.log(a,b,c); ——1,2,3 let {a:d,b:f,e,a}=obj; console.log(d,f,e,a); ——1,2,undefined,1
标签:new fun values argument 运算符 value style ret 对象
原文地址:http://www.cnblogs.com/llying/p/7528968.html