百科狗-知识改变命运!
--

Uint8Array - JavaScript TypedArray 对象

乐乐1年前 (2023-11-21)阅读数 12#技术干货
文章标签数组

Uint8Array

Uint8Array数组类型表示一个8位无符号整型数组,创建时内容被初始化为0。创建完后,可以以对象的方式或使用数组下标索引的方式引用数组中的元素。

语法格式

new Uint8Array(); // ES2017 最新语法
new Uint8Array(length); // 创建初始化为0的,包含length个元素的无符号整型数组
new Uint8Array(typedArray);
new Uint8Array(object);
new Uint8Array(buffer [, byteOffset [, length]]);

Uint8Array - JavaScript TypedArray 对象

构造语法和参数的更多信息请参见TypedArray.

属性

Uint8Array.BYTES_PER_ELEMENT返回数组中元素的字节数,Uint8Array中返回1字节。Uint8Array.length静态属性length一直为0。想获知其真实长度(元素个数),请参阅Uint8Array.prototype.length.Uint8Array.name返回构造名的字符串,对Uint8Array类型而言返回“Uint8Array”Uint8Array.prototypeTypedArray对象的原型.

方法

Uint8Array.from()从一个数组或可迭代的对象创建一个新的Uint8Array数组,可参见Array.from().Uint8Array.of()通过一个可变数目的参数创建一个新的Uint8Array数组,可参见Array.of().

Uint8Array原型声明

所有的Uint8Array对象继承自%TypedArray%.prototype.

属性

Uint8Array.prototype.constructor返回创建实例属性的函数,默认为Uint8Array构造器。Uint8Array.prototype.buffer只读返回由Uint8Array引用的ArrayBuffer,在构造时期固定,所以是只读的。Uint8Array.prototype.byteLength只读返回Uint8Array长度(字节数)。在构造时期固定,所以是只读的Uint8Array.prototype.byteOffset只读返回Uint8Array距离其ArrayBuffer起始位置的偏移(字节数)。在构造时期固定,所以是只读的Uint8Array.prototype.length只读返回保存在Uint8Array中的元素数量。在构造时期固定,所以是只读的

方法

Uint8Array.prototype.copyWithin()复制数组中的元素序列,请参见Array.prototype.copyWithin()Uint8Array.prototype.entries()返回新的Array Iterator对象,含有数组中每个下标处的键值对。请参见Array.prototype.entries().Uint8Array.prototype.every()测试数组中所有元素是否都能通过由函数提供的测试。请参见Array.prototype.every()Uint8Array.prototype.fill()使用静态值填充从起始下标到终止下标的数组元素。请参见Array.prototype.fill()Uint8Array.prototype.filter()创建新的数组,含有数组中给定过滤器返回 true 的所有元素。请参见Array.prototype.filter().Uint8Array.prototype.find()如果数组中的元素满足提供的测试函数,返回找到的值,如果没有找到则返回undefined。请参见Array.prototype.find()Uint8Array.prototype.findIndex()如果数组中的元素满足提供的测试函数,返回找到的下标,如果没有找到则返回-1。请参见Array.prototype.findIndex().Uint8Array.prototype.forEach()对数组的每个元素调用字符串Array.prototype.forEach()Uint8Array.prototype.includes()判断类型化数组是否包含特定值,如果包含返回true,否则返回false。另见Array.prototype.includes()Uint8Array.prototype.indexOf()返回数组中等于特定值的第一个元素(下标最小),如果没有找到则返回-1,请参见Array.prototype.indexOf().Uint8Array.prototype.join()将数组中所有元素连接为字符串。请参见Array.prototype.join()Uint8Array.prototype.keys()返回新的Array Iterator,含有数组中每个下标的键,请参见Array.prototype.keys()Uint8Array.prototype.lastIndexOf()返回数组中等于特定值的最后一个元素(下标最大),如果没有找到则返回-1,请参见Array.prototype.lastIndexOf()Uint8Array.prototype.map()使用在该数组的每个元素上调用函数的结果创建新数组,请参见Array.prototype.map()Uint8Array.prototype.move()未实现Uint8Array.prototype.copyWithin()的之前的非标准版本。Uint8Array.prototype.reduce()对累加器和数组的每个值应用函数(从左到右),使其归约为单一的值,另见Array.prototype.reduce()Uint8Array.prototype.reduceRight()对累加器和数组的每个值应用函数(从右到左),使其归约为单一的值,另见Array.prototype.reduceRight()Uint8Array.prototype.reverse()翻转数组中的元素顺序—第一个变为最后,最后变为第一个。另见Array.prototype.reverse()Uint8Array.prototype.set()在类型化数组中储存多个值,从特定数组中读取输入。Uint8Array.prototype.slice()提取数组的某个部分并返回新的数组,请参见Array.prototype.slice()Uint8Array.prototype.some()如果数组中至少一个元素满足给定的测试函数,则返回true。请参见Array.prototype.some()Uint8Array.prototype.sort()原地排序数组中的元素,并返回该数组,请参见Array.prototype.sort()Uint8Array.prototype.subarray()从给定的元素起始和终止下标返回新的 Uint8ArrayUint8Array.prototype.values()返回新的Array Iterator对象,含有数组每个下标处的值,请参见Array.prototype.values()Uint8Array.prototype.toLocaleString()返回表示数组及其元素的本地化字符串,请参见Array.prototype.toLocaleString()Uint8Array.prototype.toString()返回表示数组及其元素的字符串。请参见Array.prototype.toString()Uint8Array.prototype[@@iterator]()返回新的 Array Iterator对象,包含数组中每个下标处的值。

例子

// 来自长度
var uint8 = new Uint8Array(2);
uint8[0] = 42;
console.log(uint8[0]); // 42
console.log(uint8.length); // 2
console.log(uint8.BYTES_PER_ELEMENT); // 1

// 来自数组
var arr = new Uint8Array([21,31]);
console.log(arr[1]); // 31

// 来自另一个 TypedArray
var x = new Uint8Array([21, 31]);
var y = new Uint8Array(x);
console.log(y[0]); // 21

// 来自 ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8Array(buffer, 1, 4);

// 来自一个迭代器 
var iterable = function*(){ yield* [1,2,3]; }(); 
var uint8 = new Uint8Array(iterable); 
// Uint8Array[1, 2, 3]

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)