【问题标题】:ArrayBuffer.prototype.slice shim for IE?用于 IE 的 ArrayBuffer.prototype.slice 垫片?
【发布时间】:2014-02-21 18:51:03
【问题描述】:

Internet Explorer 未实现 ArrayBuffer.prototype.slice。令人惊讶的是,他们don't plan on implementing it any time soon。因此,此功能是否有任何垫片?如果没有,那么这将是我或其他人实施后互联网上的规范答案。

【问题讨论】:

标签: javascript internet-explorer compatibility shim arraybuffer


【解决方案1】:

这似乎可以解决问题。接受建议。

if (!ArrayBuffer.prototype.slice) {
    //Returns a new ArrayBuffer whose contents are a copy of this ArrayBuffer's
    //bytes from `begin`, inclusive, up to `end`, exclusive
    ArrayBuffer.prototype.slice = function (begin, end) {
        //If `begin` is unspecified, Chrome assumes 0, so we do the same
        if (begin === void 0) {
            begin = 0;
        }

        //If `end` is unspecified, the new ArrayBuffer contains all
        //bytes from `begin` to the end of this ArrayBuffer.
        if (end === void 0) {
            end = this.byteLength;
        }

        //Chrome converts the values to integers via flooring
        begin = Math.floor(begin);
        end = Math.floor(end);

        //If either `begin` or `end` is negative, it refers to an
        //index from the end of the array, as opposed to from the beginning.
        if (begin < 0) {
            begin += this.byteLength;
        }
        if (end < 0) {
            end += this.byteLength;
        }

        //The range specified by the `begin` and `end` values is clamped to the 
        //valid index range for the current array.
        begin = Math.min(Math.max(0, begin), this.byteLength);
        end = Math.min(Math.max(0, end), this.byteLength);

        //If the computed length of the new ArrayBuffer would be negative, it 
        //is clamped to zero.
        if (end - begin <= 0) {
            return new ArrayBuffer(0);
        }

        var result = new ArrayBuffer(end - begin);
        var resultBytes = new Uint8Array(result);
        var sourceBytes = new Uint8Array(this, begin, end - begin);

        resultBytes.set(sourceBytes);

        return result;
    };
}

【讨论】:

  • 如果start &lt; -this.byteLength 会失败。
  • @Bergi:谢谢,修复并添加了来自 khronos.org 描述的 cmets
【解决方案2】:

根据来自ttaubert's Arraybuffer-slice github repo的 index.js 的 cmets 版本稍作调整

if (!ArrayBuffer.prototype.slice) {
  ArrayBuffer.prototype.slice = function (begin, end) {
    var len = this.byteLength;
    begin = (begin|0) || 0;
    end = end === (void 0) ? len : (end|0);

    // Handle negative values.
    if (begin < 0) begin = Math.max(begin + len, 0);
    if (end < 0) end = Math.max(end + len, 0);

    if (len === 0 || begin >= len || begin >= end) {
      return new ArrayBuffer(0);
    }

    var length = Math.min(len - begin, end - begin);
    var target = new ArrayBuffer(length);
    var targetArray = new Uint8Array(target);
    targetArray.set(new Uint8Array(this, begin, length));
    return target;
  };
}

【讨论】:

  • 这会为ab = new ArrayBuffer(10); new Uint8Array(ab.slice(-1000, 5)) 抛出异常,而不是给出从 0 到 5 的切片
猜你喜欢
  • 2015-07-06
  • 2012-06-10
  • 2022-11-17
  • 1970-01-01
  • 1970-01-01
  • 2021-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多