typedarrays splice

TypedArrays 不是一个典型的 数组类型,所以不存在 splice 方法。但是可以模拟实现

function splice(arr, starting, deleteCount, elements) {
  if (arguments.length === 1) {
    return arr;
  }
  starting = Math.max(starting, 0);
  deleteCount = Math.max(deleteCount, 0);
  elements = elements || [];


  const newSize = arr.length - deleteCount + elements.length;
  const splicedArray = new arr.constructor(newSize);

  splicedArray.set(arr.subarray(0, starting));
  splicedArray.set(elements, starting);
  splicedArray.set(arr.subarray(starting + deleteCount), starting + elements.length);
  return splicedArray;
};
posted @ 2017-07-24 19:49  buzzjan  阅读(201)  评论(0编辑  收藏  举报