[TOC]数组
//声明 var str = ``; //嵌入变量 var str = `${变量}`; // username + "___" + password // `${username} ---- ${password}`
var username = 'jack'; var age = 18; console.info(`你的姓名是${username} , 你的年龄是${age}`);
const people = { name: 'lux', age: 20 } const name = people.name; //ES5写法 const age = people.age; console.log(name + ' ‐‐‐ ' + age)
let {变量1,变量2,...} = 对象; //例如: {} = {}
let student = { name : "张三", age : 21, course : { en : 100, math : 99 } }; let {name,age} = student; console.info(name + "_" + age); let {course : {en , math }} = student; console.info(en + "_" + math );
let [变量1,变量2,...] = 数组; //例如: [] = []
let arr = ['安徽','滁州']; let [province,city] = arr; //arr[0]赋值给province,arr[1]赋值给city console.info(province); console.info(city); let [pro,cit,cou = '全椒'] = arr; //arr[3]若是不存在,就使用默认值’全椒’ console.info(province); console.info(city); console.info(province); console.info(cou);
[x, y] = [y, x];
var map = new Map(); map.set('first', 'hello'); map.set('second', 'world'); for (let [key, value] of map) { console.log(key + " is " + value); } // first is hello // second is world
const { SourceMapConsumer, SourceNode } = require("source-map");
function 函数名(参数名 = 默认值){ }
function fun1({x = "x1" , y } = {y : "y2"}){ return [x , y] ; } console.info( fun1() ); //[ 'x1', 'y2' ] console.info( fun1({}) ); //[ 'x1', undefined ] , //{} 覆盖 {y:"y2"} ,解构默认值,x=x1,y=undefined
function fun2(args = new Error("参数必须填写")){ console.info(args); } fun2(); fun2("abc");
对象简写浏览器
//属性名和属性值(变量)相同时,能够简写成一个。注意:不能使用双引号 //匿名函数,能够省略function关键字 var user = { username, // "username" : username password, show () { } }
(参数1, ….) => { //语句 return 返回值; }
var fn = function(a,b){ //ES5定义匿名函数的方式 return a+b; } var fn2 = (a,b) =>{ //ES6箭头函数匿名函数 return a + b; }
var fn2_1 = () => { console.info("没有参数"); } var fn2_2 = (a) => { console.info("一个参数"); } var fn2_1 = (a,b) => { console.info("多个参数"); } var fn2_3 = a => { //省略写法 console.info("一个参数"); }
var fn3_1 = (a,b) => {//普通语句 console.info("普通语句"); } var fn3_2 = (a,b) => { return a+b;//return语句 } var fn3_3 = (a,b) => console.info("普通语句"); //普通语句的省略写法 var fn3_4 = (a,b) => a+b; //return语句的省略写法,必须省略return
var name = "rose"; var person = { name : 'jack', show : function(){ console.info( this.name ); }, show2 : () => { console.info( this.name ); } }; person.show(); //输出jack person.show2(); //输出undefined,在Node.js中没有window对象,没法得到对应值 //若是但愿得到jack,必须经过person.name方式得到
建立 new Map(); 设置数据 map.set(k,v); 得到数据 let v = map.get(k) 是否存在 let b = map.has(k) 删除数据 map.delete(k)
//Set集合:存储惟一数据 建立 new Set() 添加数据 set.add(val)
var arr = [2, 3, 5, 4, 5, 2, 2]; //方式1 var set2 = new Set(); // 1) 遍历数组,将数据添加到set arr.map( s => set2.add(s) ); // 2) 遍历set集合,添加到新数组 var arr2 = []; set2.forEach( v => arr2.push(v) ); console.info( arr2 ); //[ 2, 3, 5, 4 ] //方式2 var arr3 = [ ... new Set(arr) ] console.info( arr3 ); //[ 2, 3, 5, 4 ]
//遍历数组 for(let a of arr4){ console.info(a); } //遍历Map,for…of与解构结合遍历Map for(let [k,v] of map4){ console.info(`输出的数据键是${k}值是${v}`); } //遍历Set for(let s of set4){ console.info(s); } //自定义对象不能遍历,须要借助keys转换成“键数组” for(let key of Object.keys(obj4)){ console.info(`对象的键是${key},值是${obj4[key]}`); } //也能够借助entries转换成“键值对” for(let [k,v] of Object.entries(obj4)){ console.info(`entries : 对象的键是${k},值是${v}`); }
遍历方式 | 描述 | 实例 |
---|---|---|
for循环遍历 | 普通循环,经常使用于处理数组 | for (let i = 0;i < array.length;i++){ |
map() | 数组链式操做函数 | array.map( fn ).xxx() |
forEach() | 简化数组、Map、Set的遍历 | xxx.forEach( fn ) |
for…in | 任意顺序遍历一个对象的可枚举属性 | for(let xx in obj) {} |
for…of | 不一样的数据结构提供统一的访问机制 | for(let xx of obj) {} |
函数名(参数1, 参数2, …可变) function add(...num){ //可变参数num,就是一个数组,运行时存放了全部的实参 var sum = 0 ; num.forEach( i => sum += i); return sum; } console.info( add(1,2,3) ); function count(args,...other){ console.info(arguments.length); //虽有参数的个数,伪数组,不能使用forEach进行遍历 console.info(other.length); //可变参数的个数,真数组,可使用forEach进行遍历 } count(1,2,3);
扩展运算符(spread)是三个点(...)。它比如rest参数的逆运算,数据结构
操做数据是数组,将一个数组转为用逗号分隔的参数序列。函数
操做数据是对象,取出参数对象的全部可遍历属性,拷贝到当前对象之中学习
扩展运算符ui
//将对象或数组彻底拆分 ...数组 --> 多个元素 ...对象 --> 多个属性对 var arr = ['a','b','c']; function fun3(x,y,z){ console.info( [x,y,z] ); } fun3( arr ); //[ [ 'a', 'b', 'c' ], undefined, undefined ] fun3( ...arr ); //[ 'a', 'b', 'c' ] let z = { a: 3, b: 4 }; let n = { ...z }; // { a: 3, b: 4 }
var arr1 = ['a', 'b']; var arr2 = ['c']; var arr3 = ['d', 'e']; //经过 concat函数合并 console.info( arr1.concat(arr2 ,arr3 ) ); //经过扩展运算符合并 console.info( [...arr1 ,...arr2 ,...arr3] );
//经过split拆分字符串 console.info( "abc".split("") ); //经过扩展运算符拆分 console.info( [..."abc"] );
平常学习的总结,主要是为了本身之后看,固然你们有什么好的建议,欢迎评论留言。this