非常教程

JavaScript参考手册

全球对象 | Global Objects

Uint8ClampedArray

Uint8ClampedArray(8位无符号整型固定数组)类型化数组表示一个由值固定在0-255区间的8位无符号整型组成的数组;如果你指定一个在 [0,255] 区间外的值,它将被替换为0或255;如果你指定一个非整数,那么它将被设置为最接近它的整数。(数组)内容被初始化为0。一旦(数组)被创建,你可以使用对象的方法引用数组里的元素,或使用标准的数组索引语法(即使用方括号标记)。

语法

new Uint8ClampedArray(); // new in ES2017
new Uint8ClampedArray(length);
new Uint8ClampedArray(typedArray);
new Uint8ClampedArray(object);
new Uint8ClampedArray(buffer [, byteOffset [, length]]);

关于构造函数语法和参数的更多信息,参见TypedArray

属性

Uint8ClampedArray.BYTES_PER_ELEMENT返回元素大小的一个数值。对Uint8ClampedArray而言是1。

Uint8ClampedArray.length静态长度属性值为0。对于实际长度(元素的数量),见Uint8ClampedArray.prototype.length

Uint8ClampedArray.name返回构造函数名的字符串值。对Uint8ClampedArray类型而言:"Uint8ClampedArray"。Uint8ClampedArray.prototype原型是TypedArray(类型化数组)对象。

方法

Uint8ClampedArray.from()从一个类数组或可枚举对象创建一个新的Uint8ClampedArray。参见Array.from()

Uint8ClampedArray.of()通过一个可选数量参数来创建一个新的Uint8ClampedArray。参见Array.of()

Uint8ClampedArray prototype

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

属性

Uint8ClampedArray.prototype.constructor返回创建一个实例原型的函数。这是Uint8ClampedArray默认的构造函数。

Uint8ClampedArray.prototype.buffer只读返回由Uint8ClampedArray引用的ArrayBuffer。在创建时所固定下来,因此只能读取

Uint8ClampedArray.prototype.byteLength只读返回从ArrayBuffer开始的Uint8ClampedArray的(字节的)长度。在创建时所固定下来,因此只能读取

Uint8ClampedArray.prototype.byteOffset只读返回从ArrayBuffer开始的Uint8ClampedArray的(字节的)偏移。在创建时所固定下来,因此只能读取

Uint8ClampedArray.prototype.length只读返回UintClamped8Array具有的元素数量。在创建时所固定下来,因此只能读取

Methods

Uint8ClampedArray.prototype.copyWithin()复制数组中的一系列数组元素。另见Array.prototype.copyWithin()

Uint8ClampedArray.prototype.entries()返回Array Iterator包含数组中每个索引的键/值对的新对象。另见Array.prototype.entries()

Uint8ClampedArray.prototype.every()测试数组中的所有元素是否都通过了函数提供的测试。另见Array.prototype.every()

Uint8ClampedArray.prototype.fill()用静态值填充从开始索引到结束索引的数组的所有元素。另见Array.prototype.fill()

Uint8ClampedArray.prototype.filter()使用提供的过滤函数返回true的数组的所有元素创建一个新数组。另见Array.prototype.filter()

Uint8ClampedArray.prototype.find()如果数组中的元素满足提供的测试函数或者undefined未找到,则返回数组中找到的值。另见Array.prototype.find()

Uint8ClampedArray.prototype.findIndex()如果数组中的元素满足提供的测试函数,则返回数组中找到的索引;如果未找到,则返回-1。另见Array.prototype.findIndex()

Uint8ClampedArray.prototype.forEach()为数组中的每个元素调用一个函数。另见Array.prototype.forEach()

Uint8ClampedArray.prototype.includes()确定类型数组是否包含某个元素,返回true还是false适当的。另见Array.prototype.includes()

Uint8ClampedArray.prototype.indexOf()返回数组中等于指定值的元素的第一个(最少)索引,如果没有找到,则返回-1。另见Array.prototype.indexOf()

Uint8ClampedArray.prototype.join()将数组的所有元素连接成一个字符串。另见Array.prototype.join()

Uint8ClampedArray.prototype.keys()返回Array Iterator包含数组中每个索引的键的新键。另见Array.prototype.keys()

Uint8ClampedArray.prototype.lastIndexOf()返回数组中等于指定值的元素的最后一个(最大)索引,如果没有找到,则返回-1。另见Array.prototype.lastIndexOf()

Uint8ClampedArray.prototype.map()创建一个新的数组,并在该数组中的每个元素上调用一个提供的函数的结果。另见Array.prototype.map()

Uint8ClampedArray.prototype.move()UnimplementedFormer的非标准版本Uint8ClampedArray.prototype.copyWithin()

Uint8ClampedArray.prototype.reduce()对累加器和数组的每个值应用一个函数(从左到右)以将其减少到单个值。另见Array.prototype.reduce()

Uint8ClampedArray.prototype.reduceRight()对累加器和数组的每个值(从右到左)应用一个函数,以将其减少为单个值。另见Array.prototype.reduceRight()

Uint8ClampedArray.prototype.reverse()颠倒数组元素的顺序 - 第一个变成最后一个,最后一个变成第一个。另见Array.prototype.reverse()

Uint8ClampedArray.prototype.set()在类型数组中存储多个值,从指定的数组读取输入值。

Uint8ClampedArray.prototype.slice()提取数组的一部分并返回一个新的数组。另见Array.prototype.slice()

Uint8ClampedArray.prototype.some()如果此数组中至少有一个元素满足提供的测试函数,则返回true。另见Array.prototype.some()

Uint8ClampedArray.prototype.sort()对数组中的元素进行排序并返回数组。另见Array.prototype.sort()。从给定的开始和结束元素索引中Uint8ClampedArray.prototype.subarray()返回一个新Uint8ClampedArray的。

Uint8ClampedArray.prototype.values()返回Array Iterator包含数组中每个索引值的新对象。另见Array.prototype.values()

Uint8ClampedArray.prototype.toLocaleString()返回表示数组及其元素的本地化字符串。另见Array.prototype.toLocaleString()

Uint8ClampedArray.prototype.toString()返回表示数组及其元素的字符串。另见Array.prototype.toString()

Uint8ClampedArray.prototype[@@iterator]()返回一个新的Array Iterator 包含数组中每个索引值的对象。

示例

不同的方式来创建一个Uint8ClampedArray

// From a length
var uintc8 = new Uint8ClampedArray(2);
uintc8[0] = 42;
uintc8[1] = 1337;
console.log(uintc8[0]); // 42
console.log(uintc8[1]); // 255 (clamped)
console.log(uintc8.length); // 2
console.log(uintc8.BYTES_PER_ELEMENT); // 1

// From an array
var arr = new Uint8ClampedArray([21,31]);
console.log(arr[1]); // 31

// From another TypedArray
var x = new Uint8ClampedArray([21, 31]);
var y = new Uint8ClampedArray(x);
console.log(y[0]); // 21

// From an ArrayBuffer
var buffer = new ArrayBuffer(8);
var z = new Uint8ClampedArray(buffer, 1, 4);

// From an iterable 
var iterable = function*(){ yield* [1,2,3]; }(); 
var uintc8 = new Uint8ClampedArray(iterable); 
// Uint8ClampedArray[1, 2, 3]

规范

Specification

Status

Comment

Typed Array Specification

Obsolete

Superseded by ECMAScript 2015.

ECMAScript 2015 (6th Edition, ECMA-262)The definition of 'TypedArray constructors' in that specification.

Standard

Initial definition in an ECMA standard. Specified that new is required.

ECMAScript Latest Draft (ECMA-262)The definition of 'TypedArray constructors' in that specification.

Draft

ECMAScript 2017 changed the Uint8ClampedArray constructor to use the ToIndex operation and allows constructors with no arguments.

浏览器兼容性

Feature

Chrome

Edge

Firefox (Gecko)

Internet Explorer

Opera

Safari

Basic support

7.0

(Yes)

4.0 (2)

11 (as of KB2929437)

11.6

5.1

new is required

?

?

44 (44)

?

?

?

Iterable in constructor

?

?

52 (52)

?

?

?

Constructor without arguments

?

?

55 (55)

?

?

?

Feature

Android

Chrome for Android

Edge

Firefox Mobile (Gecko)

IE Mobile

Opera Mobile

Safari Mobile

Basic support

4.0

(Yes)

(Yes)

4.0 (2)

(Yes)

11.6

4.2

new is required

?

?

?

44.0 (44)

?

?

?

Iterable in constructor

?

?

?

52.0 (52)

?

?

?

Constructor without arguments

?

?

?

55.0 (55)

?

?

?

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络