1.字符串相关扩展 */
let str="hello world";
console.log(str.includes('world',6));//返回true
console.log(str.startsWith('he'));
console.log(str.endsWith('d'));
2.模板字符串
// let obj={username:'哈哈',age:18};
// let str1=`
//<div>
// <span>${obj.username}</span>
// <span>${7+8}</span>
//</div>`;
// console.log(str1);
3函数的扩展
//参数默认值
function fun(param){
let sum=param ||'hello';//默认参数
console.log(sum);
}
fun();//打印hello
fun(123);//打印123
//简化版
function fun1(param='hello'){
console.log(param);
}
fun1();//打印hello
fun1(133);//打印133
//参数的解构赋值
function fun2({username,age}){
console.log(username,age);
}
// fun2();//报错
fun2({});//undefined undefined
function fun3({username='lisi',age=23}){
console.log(username,age);
}
fun3({});//lisi 23
//剩余参数rest(表示紧挨着的剩余的数据)
function fun4(a,...rest){
console.log(rest);
}
fun4(1,2,3,34);//rest加...后结果为[ 2, 3 ]
//扩展运算符
function fun5(a,b,c,d,...param){
console.log('剩余的参数:'+param);
}
fun5(1,2,3,4,4);//剩余的参数:4
fun5(1,2,3,4);//剩余的参数:
//将一个数组变成实参序列
let arr=[4,2,5,7,8];
fun5(...arr);//剩余的参数:8
//使用扩展运算符展开数组代替apply方法,将数组转为函数的参数
Math.max.apply(this,[1,2,3]);
Math.max(...[1,2,3]);
//拼接数组的作用
let arr1=[12,3,4,5];
let arr2=[22,45];
arr1.push(...arr2)
console.log(arr1);//[ 12, 3, 4, 5, 22, 45 ]
// let arr3=[...arr1,...arr2];
// console.log(arr3);//[ 12, 3, 4, 5, 22, 45 ]
```文件操作案例
const path=require(‘path’);//加载path模块
const fs=require(‘fs’);//加载fs系统模块
let root=‘C:\Users\HP\Desktop’;//拿到桌面的地址
//初始化数据
let initData={
projectName:‘myDemo’,
data:[
{name:‘css’,type:‘dir’},
{name:‘img’,type:‘dir’},
{name:‘js’,type:‘dir’},
{name:‘index.html’,type:‘file’}
]
};
// 使用反应号
let fileContent=`