javascript - IE 的 ArrayBuffer.prototype.slice 垫片?

标签 javascript internet-explorer compatibility shim arraybuffer

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

最佳答案

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

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;
    };
}

关于javascript - IE 的 ArrayBuffer.prototype.slice 垫片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21440050/

相关文章:

internet-explorer - 为什么 colorzilla 渐变在 IE 中不起作用?

python - 发布后 View 不刷新

c++ - 我应该将哪个 Boost 版本与支持 C++03 的编译器一起使用?

c++ - C++ 二进制代码能否通过 native C 接口(interface)变得可移植?有什么限制?

java - test 和 src 目录/代码之间的差异在 test 文件夹中不起作用

javascript - 使用 TAU 库在 Tizen Web 应用程序中解析 JSON

javascript - 使用window.print时如何设置页面高度?

javascript - 无法访问模板中的范围数据

javascript - 使用 Jquery 或 Javascript 循环浏览一堆小头像的最佳方法

java - IE缓存ajax请求