JavaScript ArrayBuffer 切片在 Safari 9.1.2 中明显损坏

2024-02-19

Safari 9.1.2 (10601.7.7) 中的基本 JavaScript 功能似乎被破坏。也许我只是做错了什么?正在寻求有关如何度过这一切的建议......

有问题的函数是ArrayBuffer.prototype.slice() https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ArrayBuffer/slice

下面是一个在 Chrome 和 Firefox 中运行良好的使用示例,但在 Safari 中则不行。

var buffer = new ArrayBuffer(16);
var bufferView = new Uint8Array(buffer);
console.log(bufferView.slice(0,8)); // TypeError: bufferView.slice is not a function

我只是把这个写出来作为答案,所有相关的事实都已经在评论中了(因此是社区维基)。

你正在呼唤.slice() on the Uint8Array对象,不在ArrayBuffer, and .slice()Safari 和 Internet Explorer 中的类型化数组不支持 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/slice.

相反,您可以使用bufferView.buffer.slice(),或者 Patch 编写的这个辅助方法:

if(!Uint8Array.prototype.slice)
{
    Uint8Array.prototype.slice = function(a,b){
        var Uint8ArraySlice = new Uint8Array(this.buffer.slice(a,b));
        return Uint8ArraySlice;
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

JavaScript ArrayBuffer 切片在 Safari 9.1.2 中明显损坏 的相关文章

随机推荐