ES6复习 let const 新增API 扩展运算符

Let const定义变量不能重复声明 暂时性死区 没有预解析

function(x){let x=7
x=9}

报错  等同于

function(x){
let x=7
let x=9}

遍历字符串 首选for of 范围更广                                         

var str = 'b0232'
for (let word of str) {
        console.log('for of', word);
    }

Includes startWith padStart endWith padEnd repeat

//Includes startWith padStart endWith padEnd
	var str = '12abc23'
    console.log(str.includes('abc'));//true
    console.log(str.startsWith('23', 5));//true
    console.log(str.endsWith('bc', 5));//true
    console.log(str.repeat(2)); //12abc2312abc23
    console.log(str.padStart(10, '2345')); //23412abc23
    console.log(str.padEnd(8, '2345')); //12abc232

模板字符串

{//传统

    const name = 'lily'
    const age = 19
    const desp = "我的名字是" + name + ',我今年' + age + '岁'
    console.log(desp);
//模板字符串 反引  可以保留换行
    const desp2 = `
    我的名字是${name},
    我今年${age}岁
    `
    console.log(desp2);
}

扩展运算符的使用

//复制
    const list1 = [1, 2, 3]
   // const list2 = [list1]//存的是地址 后期改变list1 list2的值也会改变
    const list2 = [...list1] //复制数组 后期改变list1 list2的值不改变
//分割
  const list = [1, 'a', 'b']
  const [, ...newlist] = list
  console.log(newlist);// ["a", "b"]
// 给函数传递参数
    function fn(x, y) {}
    let arr = [1, 2, 3]
    console.log(fn(...arr)); //3多出来的参数不影响 无形参接收参数

 fill替换数组中的元素

let arr = [1, 2, 4, 6, 7]
let arr2 = arr.fill(3, 2, 4) //(替换值,[开始替换的位置,结束替换的位置)) 左闭右开
console.log(arr2); //(5) [1, 2, 3, 3, 7]

concat()方法用于连接两个数组  参数可以是数组 可以是具体的值 

    var a = [1]
    var c = a.concat(4, 5)
    console.log(c);//[1, 4, 5]
    var d = a.concat([2, 3, 4])
    console.log(d);//[1,2,3,4]

flat展开数组

   let arr4 = arr.flat()
   let arr2 = arr.flat(2) //展开两层  参数为想要展开的层数 默认展开一层

find findIndex

 //find
    let arr = [{ 'name': 'lily', id: 1 }, { 'name': 'simon' }, { 'name': 'lily' }]
    let arr2 = arr.find(function(item) {
            return item.name === 'lily'
        }) //find函数找到一个符合条件的就停止
    console.log(arr2); //{name: "lily", id: 1}
//findIndex
    let a = arr.findIndex(function(item) {
            return item.name === 'lily'
        }) //查找符合条件的值的索引值  值不存在返回-1
    console.log(a); //0

 includes indexOf

    const arr = [1, 2, 3, 4]
    let result = arr.includes(4)
    console.log(result); //true
    let result2 = arr.indexOf(4)
    console.log(result2); //3 不存在的话返回-1

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值