ES6字符串相关拓展及文件操作案例

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=`

               `; // 创建项目路径path.join(a,b) 作用:将路径片段使用特定的分隔符(window:\)连接起来形成路径,并规范化生成的路径。  fs.mkdir(path.join(root,initData.projectName),(err)=>{   if(err)return;  // 表示路径拼接失败  终结程序  // 创建子目录和文件  initData.data.forEach((item)=>{ //   console.log(item);   if(item.type=='dir'){    // 创建子目录    fs.mkdirSync(path.join(root,initData.projectName,item.name));   }else if(item.type=='file'){    // 创建 文件写入 内容    fs.writeFileSync(path.join(root,initData.projectName,item.name),fileContent);   }  }) })

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值