ES6中copyWithin()与fill()的不同之处

复制和填充方法

ES6新增了两个方法:批量复制方法copyWithin(),以及填充数组方法fill()。这两个方法的函数签名类似,都需要指定既有数组实例上的一个范围,包含开始索引,不包含结束索引。使用这个方法不会改变数组的大小。
使用fill()方法可以向一个已有的数组中插入全部或部分相同的值。开始索引用于指定开始填充的位置,它是可选的。如果不提供结束索引,则一直填充到数组末尾。负值索引从数组末尾开始计算也可以将负值索引想象成数组长度加上它得到的一个正索引

      const zeroes = [0, 0, 0, 0, 0];
      // 用5填充整个数组
      zeroes.fill(5);
      console.log(zeroes); // [5, 5, 5, 5, 5]
      // 重置
      zeroes.fill(0);
      console.log(zeroes); // [0, 0, 0, 0, 0]
	  const zeroes = [0, 0, 0, 0, 0];
	  // 用6填充索引大于等于3的元素
      zeroes.fill(6, 3);
      console.log(zeroes); //[0, 0, 0, 6, 6]
      const zeroes = [0, 0, 0, 0, 0];
	  // 用7填充索引大于等于1且小于3的元素
      zeroes.fill(7, 1, 3);
      console.log(zeroes); // [0, 7, 7, 0, 0]
      const zeroes = [0, 0, 0, 0, 0];
      // 用8填充索引大于等于1且小于4的元素
      // (-4 + zeroes.length = 1)
      // (-1 + zeroes.length = 4)
      zeroes.fill(8, -4, -1);
      console.log(zeroes); //[0, 8, 8, 8, 0]
      const zeroes = [0, 0, 0, 0, 0];
      //fill() 静默忽略超出数组边界、零长度及方向相反的索引范围
      //索引过低,忽略
      zeroes.fill(1, -10, -6);
      console.log(zeroes); // [0, 0, 0, 0, 0]
      // 索引过高,忽略
      zeroes.fill(1, 10, 15);
      console.log(zeroes); // [0, 0, 0, 0, 0]
      // 索引反向,忽略
      zeroes.fill(2, 4, 2);
      console.log(zeroes); // [0, 0, 0, 0, 0]
      // 索引部分可用,填充可用部分
      zeroes.fill(4, 3, 10);
      console.log(zeroes); //[0, 0, 0, 4, 4]

fill()不同,copyWithin()会按照指定范围浅复制数组中的部分内容,然后将它们插入到指定索引开始的位置。开始索引和结束索引则于fill()使用同样的计算方法

      let ints,
      	  reset = () => ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
      reset();
      // 从ints中复制索引0开始的内容,插入索引5开始的位置
      // 在源索引或目标索引到达数组边界时停止
      ints.copyWithin(5);
      console.log(ints); //[0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
      reset();
      // 从ints中复制索引5开始的内容,插入到索引0开始的位置
      ints.copyWithin(0, 5);
      console.log(ints); //[5, 6, 7, 8, 9, 5, 6, 7, 8, 9]
      reset();
      // 从ints中复制索引0开始到索引3结束的内容
      // 插入到索引4开始的位置
      ints.copyWithin(4, 0, 3);
      console.log(ints); // [0, 1, 2, 3, 0, 1, 2, 7, 8, 9]
      reset();
       // JavaScript 引擎在插值前会完全复制范围内的值
      // 因此复制期间不存在重写的风险
      ints.copyWithin(2, 0, 6);
      console.log(ints); // [0, 1, 0, 1, 2, 3, 4, 5, 8, 9]
      reset();
      // 支持负索引值,fill()相对于数组末尾计算正向索引的过程是一样的
      ints.copyWithin(-4, -7, -3);
      console.log(ints); // [0, 1, 2, 3, 4, 5, 3, 4, 5, 6]
	  // copyWithin() 静默忽略超出数组边界、零长度及方向相反的索引范围
      let ints,
          reset = () => (ints = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
      reset();
      //   索引过低,忽略
      ints.copyWithin(1, -15, -12);
      console.log(ints); //  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      reset();
      //   索引过高,忽略
      ints.copyWithin(1, 12, 15);
      console.log(ints); //  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      reset();
      //   索引反向,忽略
      ints.copyWithin(2, 4, 2);
      console.log(ints); //  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
      reset();
      //   索引部分可用,复制、填充可用部分
      ints.copyWithin(4, 7, 10);
      console.log(ints); // [0, 1, 2, 3, 7, 8, 9, 7, 8, 9]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值