es6(1)
1、数组的解构赋值
1 let [a,b,c] = [1,2,3]; 2 console.log(a); 3 console.log(b); 4 console.log(c); 5 let [a,[b,c],d] = [1,[2,3],4]; 6 console.log(a); 7 console.log(b); 8 console.log(c); 9 let [foo = true] = []; 10 console.log(foo);
2、解构的默认值,注意undefined 和 null 的区别
1 let [a,b="jane"] = ['jane',null]; 2 console.log(a+b);
3、对象的结构赋值
1 let {foo,bar} = {foo:'jane',bar:'简'}; 2 console.log(foo+bar);
4、圆括号
如果在解构之前就已经定义了变量,再解构就会出错
let foo; ({foo} = {foo:'jane'}); console.log(foo);
5、字符串解构
const [a,b,c,d] = 'jane';
console.log(a);
console.log(b);
console.log(c);
console.log(d);
6、扩展运算符
对象扩展运算符,编写一个方法时,允许传入的参数是不确定的,使用对象扩展运算符来作参数
function jane(...arg){ console.log(arg[0]); console.log(arg[1]); console.log(arg[2]); console.log(arg[3]); } jane(1,2,3);
7、扩展运算符的用处
let arr1 = ['jane','chree','elaine']; let arr2 = [...arr1]; arr2.push('janeo'); console.log(arr2); console.log(arr1);
8、rest运算符
function jane(first,...arg){ console.log(arg.length); } jane(0,1,2,3);
如何循环输出arg的值, for ... of 的循环可以避免开拓空间,增加代码运行效率
function jane(first,...arg){ for(let val of arg){ console.log(val); } } jane(0,1,2);
9、字符串模板
let jspang = `jane`; let blog = `字符串模板`; let a = 1; let b = 2; let result = `${a+b}`; console.log(result);
10、查找是否存在
let jane = 'jane'; let blog = 'janeasdfg'; console.log(blog.includes(jane));
// 判断开头/结尾是否存在
blog.startsWith(jane);
blog.endsWith(jane);
// 复制字符串
console.log('jane'.repeat(3));